Previous section Review
Class
Property
instance variables (in memory of each instance)
class variables (in existence class, shared use)
Private attribute __var
Method
Construction Method __init__
destructor Method __del__ (instance open link temporary file is closed and destroyed)
Private methods
Objects: Objects that are obtained after instantiating a class
Packaging
The implementation details of some functions are not exposed to external
Continued commitment
How to Inherit
Inherited
Combination
Reuse of code
Single stage into
Multiple inheritance
2.7 Breadth first new class, depth first Classic class
3.0 is the breadth first
Class Foo (School)
def __init__ (Self,name,age,sex,salary,course):
#新式类
Super (Foo,self) __init__ (name,age,sex)
#经典类
school.__init__ (Self,name,age,sex)
#组合
Self.person = person
Self.salary = Salary
Self.course = Course
Polymorphic
Interface reuse, a multi-interface implementation
Static methods
The method of decorating is changed into a static method by @staticmethod Adorner, what is static method?
The normal method can instantiate the call, and the method can pass self. A generic instance variable or a class variable, but a static method is not accessible to instance variables and instance variables, and it is unique to the class to call this method through the class name
classDog (object):def __init__(self,name): Self.name=name#Method defEat (Self,food):Print('%s is eating%s'%(Self.name,food)) d= Dog ('Cehngronghua') D.eat ('Baozi')#output Cehngronghua is eating Baiozi##############classDog (object):def __init__(self,name): Self.name=name @staticmethod#what is the relationship to a class, just the equivalent of a function defEat (Self,food):Print('%s is eating%s'%(Self.name,food)) d= Dog ('Cehngronghua') d.eat (d,'Zhiyu')#independent function re-passing value#output Cehngronghua is eating Zhiyu
Class method
Class methods are implemented through the @classmethod adorner, the difference between a class method and a common method is that class methods can only access class variables and cannot access instance variables
#Author:zhiyu SuclassDog (object): Name= 1233def __init__(self,name): Self.name=name @classmethoddefEat (self):Print('%s is eating%s'% (Self.name,'DD')) d= Dog ('Cehngronghua') d.eat ()#output 1233 is eating DD directly calls a variable of class
Property method
Turn a method into a property so call direct call without entering parentheses
#Author:zhiyu SuclassDog (object):def __init__(self,name): Self.name=name @property#Attribute Property defEat (self):Print('%s is eating%s'% (Self.name,'DD')) d= Dog ('Cehngronghua')#d.eat () #调用时会报错 ' Nonetype ' object is not callabled.eat#output Cehngronghua is eating ddProperty Method @eat.setter###################Author:zhiyu SuclassDog (object):def __init__(self,name): Self.name=name#method becomes a property@property#Attribute Property defEat (self):Print('%s is eating%s'% (Self.name,'DD')) #If you want to set a property for eat, you need to re-establish a eat@eat. Setterdefeat (self, food):Print('set to food:', food) d= Dog ('Cehngronghua')#d.eat () #调用时会报错 ' Nonetype ' object is not callabled.eatd.eat='Baozi'#output Cehngronghua is eating dd set to Food:baozi###################call @eat. deleter Delete#Author:zhiyu SuclassDog (object):def __init__(self,name): Self.name=name self.__food=None @propertydefEat (self):Print('%s is eating%s'% (self.name,self.__food) ) @eat. Setterdefeat (self, food):Print('set to food:', food) self.__food=Food @eat. DeleterdefEat (self):delSelf.__food Print('deleted') d= Dog ('Cehngronghua') D.eat#Call @property Eatd.eat='Baozi' #Call @eat.setterdelD.eat#call @eat. Deleter
An instance of a property method
#Author:zhiyu SuclassFlight (object):def __init__(self,name): Self.flight_name=namedefChecking_status (self):Print('cheking Flight%s tatus'%self.flight_name)return1@propertydefFlight_status (self): status=self.checking_status ()ifStatus = =0:Print('Filight got Cancelend ...') elifStatus = = 1: Print('Flighrt is arried ...') elifStatus = = 2: Print('Filght has de partured already ...') Else: Print('cannot confirm the flight staus....,please check later') @flight_status. SetterdefFlight_status (self,status):Print('Flight%s has changed status to%s'%(Self.flight_name,status)) F= Flight ('CA980') F.flight_status#to the user portF.flight_status = 2F.flight_status
View Code
Special member methods for classes
1.__DOC__ represents the description of a class
#Author:zhiyu SuclassDog (object):" "This object is a dog ." " def __init__(self,name): Self.name=name self.__food=None @propertydefEat (self):Print('%s is eating%s'% (self.name,self.__food)) d= Dog ('Cehngronghua')Print(Dog.)__doc__)#output descriptive information for the print class ' This object is a dog '
View Code
2.__module__ and __class__
__MODULE__ indicates which module the object of the current operation is in
__CLASS__ represents the class of the object that is currently being manipulated
Learn Python Basics--------6 facing object advanced