1. Object-oriented structure analysis:----Face objects are broadly divided into two areas:--------the first part: static field (static variable) part--------Part Two: Method section-Each large area can be divided into several small parts: class A:cooname = ' Jake ' # Static variable (static field) __cooage = 20 # private static variable (private static field) def __init__ (self, Name, age): # Normal Method (constructor) Self.name = name # Object properties (normal field) Self.__age = Age # Private object property (Private Normal field) def func1 (self): # normal Method Pass Def __func (self): # private method Pass @property def prop (self): # attribute Pass @classmethod # class method Def class_func (CLS): "' Definition class method with at least one CLS parameter ' Pass @staticmethod # static method Def stact_func (): ' ' defines a static method, no default parameter ' ' Pass2. Object-oriented private There are two forms of public ownership for each of the members of a class:----common members, access to----private members anywhere, only within the class to access--------private members and shared members have different access restrictions: static fields (static variables)-Common static fields: classes can be accessed, Within a class, accessible-private static fields can be accessed in derived classes: Only ordinary Fields (object properties) can be accessed within the class-common fields: objects can be accessed, classes can be accessed, accessible in derived classes-private ordinary fields: Only classes can access methods:-Common methods: objects can be accessed, A class can be accessed inside a derived class that can access-private methods: Only the inside of the class can access the summary: for these private members, they can only be used inside the class, not outside of the class and in derived classes. PS: You can pass an object if you want to access a private member. _ Class __ Property name, but absolutely not allowed!!! Why can I access it through the. _ Class __ Private member name? Because when a class is created, if a private member is encountered (including private static fields, private normal fields, private methods) itIt is automatically preceded by the _ Class name when it is saved in memory. 3. Object-oriented Members 1) field fields include: normal fields and static fields, they differ in definition and use, and the most essential difference is where the memory is saved in different places--ordinary fields belong to the object--static fields belong to the class
such as: The same point: for all methods, all belong to the class (non-object), so, in memory also save only one copy. Different points: The method callers are different, and the parameters that are passed in automatically when the method is called are different. 4.property Classmethod staticmethod1) property #将一个方法, disguised as attribute Class Bmi (object): Def __init__ (self, name, weight, Highe): Self.name = Name Self.weight = Weight Self.height = Highe @property def func (self): BMI = Self.weight/(Self.height * 2) return Bmiperson = Bmi (' Tom ', *, 1.73) s = person.namess = Person.func #将方法伪装成属性p Rint (the BIM index for '%s ' is%s '% (s, ss))--------------------------------------------------class person (): def __init__ (self, Name, age): Self.name = Name Self.__age = The age if Type (age) is int else print (' re-enter ') @property #执行查询操 Do this automatically def: return self.__age @age. Setter #执行更改操作执行此操作 def age (Self, temp): self.__ Age = Temp If the type (temp) is int else print (' re-enter ') @age. Deleter #执行del操作自动执行此方法 def Age (self): del self.__ AGE2) Classmethod----class Method: Call the method through the class name, the first parameter convention in the class method is commonly known as Cls,python automatically passes the class name to Clsclass a:def FUNC (self): # normal method print (self) @classmethod def func0 (CLS): # class method Print (CLS) a = A () A.func () # <__ main__. A object at 0x105d8a438>a.func0 () # <class ' __main__. A ' >a1 = A () a1.func0 () # object calls the class method, and the CLS obtains the class itself. ****¥ class Method Application Scenario 1-Class Some methods do not require object participation 2-changes to static variables in a Class 3-inheritance, the parent class gets the class space example of the subclass: Class A:ag E = ten @classmethod def func (CLS): Print (Cls.age) class B (A): Age = 20b.func () # 203) Staticmethod called by the class name, no default Parameters, the main role is: To make the code clear, reusable strong!
Python object-oriented structure and members