Python learns Day 9 property multi-inheritance Mixin, pythonmixin
When binding an attribute, If we expose the attribute directly, although it is easy to write, but we cannot check the parameter, so we can change the score as needed:
s = Student()s.score = 9999
To limit the score range, you can use the set_score () method to set the score and get_score () to obtain the score. In this way, in the set_score ()
On the basis of the previous code, make further changes, become the following procedure, please crossing study this program:
Copy CodeThe code is as follows:
#!/usr/bin/env python
#coding: Utf-8
Class Person:def __init__ (self, Name, email):Self.name = NameSelf.email = Email
Class Programmer (person):def __init__ (self, Name,email,lang, System, website):person.__init__ (Self,name,email)Self.lang = LangSelf.system = SystemSelf.website = website
Clas
classes and classes, then I can use the same properties and methods as members of the base class, and special methods and properties are defined in this class, so that only inherit the base class this action, you can access the properties and methods of the base class, It improves the extensibility of the code.There are pros and cons to everything: one weakness of inheritance is that there may be special classes and other special places, and a class
object of a subclass # Fish.__init__ (self) #使用super () function super (). __init__ () self.hungry=true def Eat (self): if Self.hungry: print ("Foodie's dream is to eat every day") self.hungry=false Else: print ("Too much to Eat") Shark=shark () Shark.move () #这样写的话程序会报错 because the subclass shark overrides the #__init__ method, overwriting the __init__ method of the parent class
(2)
()------------------Line----------------------2016Tesla Model sthis car has a70-kwh Battery.Sometimes some of the methods of the parent class may not have some of the attributes of the subclass, we need to reconstruct the method of the parent class, and we can redefine a method in the subclass that has the same name as the method of the parent class to be overridden. If there is a Fill_gas_tank () method in the car class, we refactor in the Electriccar. Example code:class Electriccar (Car):
http://blog.csdn.net/pipisorry/article/details/46381341There is typical use cases forSuper:In a class hierarchy Withsingle inheritance, Super can is used to refer to parent classes withoutnaming them expl Icitly, thus making the code more maintainable. This useclosely parallels the use ofSuper in other programming languages.The second use case was to support cooperative multiple inheritance in adynamic exec
Body
The first thing to be explained is that Python classes are divided into classic classes and new classes.The classic class is python2.2, but it's still compatible in 2.7, but the version after 3 only admits the new class.New classes can be used in later versions of python2.2
The difference between a classic class and a new class is:
The classic class is not derived from a base class by default, whereas a new class is the default derived from th
define a data type. The data types we define are the same data types that python comes with, such as STR, list, dict:
A = List () # A is the list type B = Animal () # B is the Animal type C = Dog () # C is the dog type
Determining whether a variable is a type can be judged by isinstance ():
>>> isinstance (A, list) true>>> isinstance (b, Animal) true>>> isinstance (c, Dog) True
It seems that a, B and C do correspond to the list, Animal, dog 3 typ
data type. When we define a class, we actually define a data type. The data types we define are the same data types that python comes with, such as STR, list, dict:# A is a list type # B is the animal type # c is the dog typeDetermining whether a variable is a type can be judged by isinstance ():>>> isinstance (A, list) True>>> isinstance (b, Animal) True>>> Isinstance (c, Dog) TrueIt seems that a, B and C do correspond to the list, Animal, dog 3 ty
1 inheritanceConcept of inheritance: inheritance is a way to create new classes, in Python, a new class can inherit from one or more parent classes, the original class is called a base class or a superclass, and a new class is called a derived class or subclass.The inheritance of classes in
object-oriented programming three elements: encapsulation, inheritance, and polymorphism. This paper mainly looks at the concepts related to encapsulation and inheritance, and the concept of polymorphism in Python is rather vague, this article does not discuss. 1 packageEncapsulation : The data and operations are assembled together, exposing only a few interfaces
data types we define are the same data types that python comes with, such as STR, list, dict:a = list() # a是list类型b = Animal() # b是Animal类型c = Dog() # c是Dog类型Judging whether a variable is a type can be isinstance() judged by:>>> isinstance(a, list)True>>> isinstance(b, Animal)True>>> isinstance(c, Dog)TrueIt seems that a, B and C do correspond to the list, Animal, dog 3 types.But wait, try it:>>> isinstance(c, Animal)TrueLooks like C's not just dog,c
Package EnclosureEncapsulation refers to the implementation details of a hidden class, which makes it easier for others to call.Purpose of Encapsulation:Allows the consumer to manipulate the object by indirectly invoking the method or property.So when Python uses encapsulation, it must use private properties and methods.Private properties and methods:A private member that starts with an ' __ ' double underscore and does not end with a double-declining
InheritedWhat is inheritanceThe so-called inheritance refers to the same class of objects to provide a common method of processing, subclasses inherit the common things of the parent class. This facilitates the reusability of the code, that is, the subclass has a method of the parent class. A new class created through inheritance is called a subclass or derived class, and the inherited class is called the b
Python multi-Inheritance
There are two typical use casesSuper:
In a class hierarchy withsingle inheritance,SuperCan be used to refer to parent classes withoutnaming them explicitly, thus making the code more maintainable. This useclosely parallels the useSuperIn other programming languages.
The second use case is to support cooperative multiple
. When we define a class, we actually define a data type. The data types we define are the same data types that python comes with, such as STR, list, dict:# A is a list type # B is the animal type # c is the dog typeJudging whether a variable is a type can be isinstance() judged by:>>> isinstance (A, list) True>>> isinstance (b, Animal) True>>> Isinstance (c, Dog) TrueIt seems a , b and indeed corresponds to, c list Animal Dog these 3 kinds.But wait,
This article mainly introduces the definition, inheritance and use object of the class in Python, and analyzes the related concepts and using techniques of Python in detail, which has some reference value, and the friends who need it can refer to the following
The examples in this article describe the definition, inheritance
Python class inheritance and refactoring 0 objects
An instance of a data structure defined by a class.
The object consists of two data members (class variables and instance variables) and methods.
#!/usr/bin/python# -*- coding: UTF-8 -*- class Employee: ‘所有员工的基类‘ empCount = 0 def __init__(self, name, salary): self.name = name se
Inherited:The object class is inherited by default in Python3. But the class that inherits the object class and subclass is called the new class (This is all in Python3). There is no inheritance called the Classic class (the object is not inherited in Python2 and his subclasses are classic classes.) )Inheritance is a class-to-class relationship that solves the problem of code reuse and reduces redundant cod
Related concepts:
Mro:method resolution order, the analytic sequence of methods, is the algorithm used in Python to deal with two semantic problems.
Ambiguity
Python supports multiple inheritance, and multiple-inheritance languages tend to encounter the following two classes of two-semantic problems: two base classes
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.