python class definitions to explain

python is how to define classes, read the following article we will, needless to say, to start learning.
A class definition:
copy the code code is as follows:

class <类名>:
 <语句>

After the class is instantiated, you can use its properties, in fact, after you create a class, you can access its properties by the class name. If modify its properties directly using the class name, it will directly affect the objects instantiated

Class private property:
  __private_attrs two begins with an underscore, stating that the property is private and can not be used in class or direct access to the outside. When used in a method of the class internal self .__ private_attrs
class methods
  within the class, the use of def keyword can be defined as a class method, and is generally different function definitions, methods must include the class parameter self, and as the first parameter
Private class method
  __private_method two begins with an underscore, the method is declared as a private method, external calls can not be in class. Call slef .__ private_methods within the class

Proprietary methods class:
the init constructor called when the object generated
del destructor, used when the object is released
repr printing, conversion
__setitem__ index assignment according
__getitem__ acquired by index value
__len__ length obtained
__cmp__ comparison operation
__call__ function transfer

__add__ add operations
__sub__ subtraction
__mul__ multiplication
__div__ divide operations
__mod__ remainder operation
__pow__ called party
copy the code code is as follows:

Class definition

class people: 
    #定义基本属性 
    name = '' 
    age = 0 
    #定义私有属性,私有属性在类外部无法直接进行访问 
    __weight = 0 
    #定义构造方法 
    def __init__(self,n,a,w): 
        self.name = n 
        self.age = a 
        self.__weight = w 
    def speak(self): 
        print("%s is speaking: I am %d years old" %(self.name,self.age)) 


p = people('tom',10,30) 
p.speak()

Second, the derived class defines:
1. Single inheritance
copy the code following code:

class <类名>(父类名)
   <语句>

Copy the code code is as follows:

class childbook(book)
    age = 10

Copy the code code is as follows:

Single inheritance example

class student(people): 
    grade = '' 
    def __init__(self,n,a,w,g): 
        #调用父类的构函 
        people.__init__(self,n,a,w) 
        self.grade = g 
    #覆写父类的方法 
    def speak(self): 
        print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))      

s = student('ken',20,60,3) 
s.speak()

2. Class multiple inheritance
copy the code code is as follows:

class 类名(父类1,父类2,....,父类n)
     <语句1>

Note that the order in parentheses parent class, if the parent class have the same method name, but did not specify when subclasses, when searching python from left to right, that method is not found in a subclass, from left to right Find the parent class contains methods
copy the code following code:

Multiple inheritance before another class, ready

class speaker(): 
    topic = '' 
    name = '' 
    def __init__(self,n,t): 
        self.name = n 
        self.topic = t 
    def speak(self): 
        print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic)) 

Multiple Inheritance

class sample(speaker,student): 
    a ='' 
    def __init__(self,n,a,w,g,t): 
        student.__init__(self,n,a,w,g) 
        speaker.__init__(self,n,t) 

test = sample("Tim",25,80,4,"Python") 
test.speak()#方法名同,默认调用的是在括号中排前地父类的方法

Guess you like

Origin www.cnblogs.com/abdm-989/p/11951260.html