1 #Encoding=utf-82 3 #Python Advanced Programming4 5 #Python Object-oriented6 #Create Class7 8 #inadvertently reduced Visual Studio Code window, checked, you can use the ctrl+= magnification window, using ctrl+-Zoom out window9 Ten One A classPerson:#the declaration of the class here uses class - 'The baseclass of Human' #document string, equivalent to the description of the class -Ins_count=0#What is stated here is the class variable, which is common in all instantiated objects, similar to what we call static variables the - def __init__(self,name,age,sex):#the declaration of the method here is to initialize the constructor with def,__init__ (), noting that the init here is double-underlined -Self.name=name#the declaration of a class variable, similar to our property, is accessed only in the current instantiated object -Self.age= Age +self.sex=Sex -Self.ins_count+=1 + A defLoginfo (self):#The definition of the method, which uses similar person.loginfo (), is a wrong way to call at if(self.sex==1):#Self represents an instance of a class, which is necessary when you define a method of a class, but you do not have to pass in the appropriate arguments when calling - Print 'His name is', Self.name,', age is', Self.age - Else: - Print 'Her name is', Self.name,', age is', Self.age#by the symbol "." To implement access to a property - PrintSelf.ins_count - in defLogage (self):#Common Methods - Print ' Age is', Self.age to + @staticmethod - defPrintclassname ():#static methods cannot access class and instance properties the Print ' Person' * $ @classmethodPanax Notoginseng defPrintattr (CLS):#class methods can access class properties cannot access instance properties - PrintCls.ins_count the + def __del__(self): A Print 'Drop' the +P=person ('Hanmeimei', 13,0)#Use the person () Form to create an instance of the class, without having to instantiate it with new as in other object-oriented languages - Person.printclassname () $ P.loginfo () $ - - Print(GetAttr (P,'name')) the - 'built-in properties of the Python class: __dict__, Class properties (including a dictionary consisting of the class's property data)'Wuyi PrintPerson.__dict__ #data properties of the output class the PrintPerson.__name__ #Output Class name - PrintPerson.__module__ #the module where the output class resides Wu PrintPerson.__doc__ #document string for output class - PrintPerson.__bases__ #the constituent tuples of all the parents of the output class About $ #del P # When the object is created, a reference is created, and when the object is not needed, the interpreter will reclaim the memory space occupied by the garbage object at the appropriate time and define the __DEL__ function to be called when the object is destroyed - - classStudent (person):#The left is the class's inheritance, using Class child (parent) - __serialno=10086#declare private variables that are not accessible externally A defLogage (self):#if the method of the parent class does not meet the requirements, you can override the parent class's methods in the child class + if(self.sex==1): the Print ' He is', Self.age - Else: $ Print ' She is', Self.age the the def __addserialno(self):#define private methods that cannot be accessed externally theSelf.__serialno+=1 the - #a description of the underscore, such as __foo () __ This is a special method of the head and tail of the double underline, generally represents the system-defined name, such as __init () __, _foo () The beginning of a single underline this is the protected level, __foo () A double underscore starts with the private level in the theS=student ('Lilei', 15,1) About S.loginfo () the s.logage () the the #familiar with the object-oriented comrades may find there is no overloading, the author said yes, I do not, and my idea does not match, ...
Python notes (ix)