Members of the class
- Field
- Normal field
Save in object, execute only through object access
- Static fields
stored in the class, execution can be accessed through the class or through the object
- Method
- Common methods
stored in a class, called by an object.
- Static methods
Static methods, which are stored in the class, are called directly by the class, or can be called by the object
- Class method
stored in a class, called directly by a class, or by an object
- Property
- Methods that can be executed without parentheses
classFoo:name='nathaniel_ static fields'#static fields def __init__(self): Self.age='19_ Normal Fields'#normal field#static fields, which are stored in the class, can be accessed through the class or accessed through the objectPrint('accessing static fields directly from a class', foo.name) obj=Foo ()Print('accessing static fields through objects', Obj.name)#normal fields, stored in objects, execute only through object accessPrint('Normal fields can only be accessed through objects', Obj.age)classFoo1:deffoo (self):Print('Common Methods') @staticmethod # using adornersdeffoo1 ():Print('Static Methods') @classmethoddefFoo2 (CLS):#CLS class name Print('class Method')#class method, stored in the class, called directly by the class, without instantiating the classFoo1.foo2 ()#static methods, which are stored in the class, are called directly by the class, without instantiating the classfoo1.foo1 ()#a common method, stored in a class, called by an object.obj =Foo1 () Obj.foo ( )#class methods and static can also be called by an objectobj.foo1 () Obj.foo2 ( )
# Properties
# Properties
# when defining, add @property adorners on the basis of common methods;
# when defined, the property has only one self parameter
# when called, no parentheses are required
# Method: Obj.func ()
# Properties: Obj.func
classFoo2: @propertydefFoo2 (self):Print('Properties') @foo2. SetterdefFoo2 (self,values):Print('Camouflage Assignment', values,) @foo2. DeleterdefFoo2 (self):Print('Disguise Delete')#property is a method, but is not bracketed when calledobj =Foo2 () Obj.foo2 # automatically executes the @property decorated Foo2 method and gets the return value Obj.foo2='123' # automatically executes the @foo2. Setter-Modified Foo2 method and assigns ' 123 ' to valuesdelObj.foo2 # Automatic execution of @foo2. Deleter Modified Foo2 Method#the execution of the above code actually has a correspondence relationship#Obj.foo2 functions corresponding to the @property#Obj.foo2 = ' 123 ' corresponds to @foo2. Function under Setter # requires a parameter#del Obj.foo2 corresponds to the function of the @foo2. Deleter
Attention:
- The classic class has only one @ propertyadorner, which corresponds to the Obj.foo2 in the above example
- There are three kinds of @ propertydecorators in the new class, that is, three of the above examples are available
Member modifiers for class
In general, there can be two types of members in each class
- Public members, accessible from anywhere
- Public fields
- Public methods
- Private members can only be accessed within the class # Private members start with a double underscore (except for special members, __init__, __call__, __del__, and so on)
- Private fields
- Private methods
- Private members of a class do not inherit from the quilt class
classFoo4:__static_foo='123' #static private fields, accessible only internally def __init__(self,name,age): Self.name= Name#Public FieldsSelf.__age= Age#private field, accessible only internally def __foo41(self):#Private method, accessible only within the class Print('Private method , accessible only within the class, private method can also access private field%s'%self.name)defShow (self):#private fields and private methods can be accessed inside a class Print(self.)__static_foo) Print(self.)__age) self.__foo41() obj= Foo4 ('Nathaniel',' -')Print(Obj.name)#public fields can be accessedPrint(obj.__age)#private field, errorObj.show ()#public methods can accessObj.__foo41()#Private method, error
Special member of Class 1, __init__ constructor method class () Automatic execution
2. __call__ object () class () () automatic execution
3, __int__ int (object) automatically executes the __int__ method in the object and assigns the return value to it
4. __str__ Str (object) automatically executes the __int__ method in the object and assigns the return value to it
5. When the __add__ two objects are added, the __add__ method of the first object is executed automatically, and the second object is passed as a parameter
6. __dict__ object. __dict__ returns the members of the object as a list. __DICT__ returns the members of the class in a dictionary
7, __del__ destructor method, the object is destroyed when executed
8. __getitem__ Slice (slice type) or index
9, __setitem__ by index assignment value
10, __delitem__ by index Delete
classFoo5:"""__doc__ Method Printing description information for a class""" def __init__(self):Print('constructor Method Class () Automatic execution') def __call__(Self, *args, * *Kwargs):Print('object () class () () Automatic execution') def __int__(self):return123def __str__(self):return '123' def __add__(self, Other):Print(Str (1 +Other )) def __del__(self):Print('destructor, executed when the object is destroyed') def __getitem__(self):Print('object is sliced and automatically executes the __getitem__ method in the object.') def __setitem__(self, Key, value):Print('object is assigned by index, automatically executes the __setitem__ method in the object') def __delitem__(self, key):Print('object is deleted by index, and the __delitem__ method in object is executed automatically .')
Print (foo5.__dic__)
class Foo5: Pass class F0051 (FOO5): Pass = Foo5 ()print# checks whether the object obj is a class CLS object , print# Check if the F0051 class is a derived class of the Foo5 class
Supplement
Python Object-oriented (advanced article)