Python Object-oriented class members
1. Fields
Normal field belongs to Object
static fields belong to class
2. Methods
- Common Methods
- the trigger is the object in parentheses with at least one argument Self = current object , can have multiple parameters
- class Method
- @classmethod (cls= current class ) The trigger is a class, called directly with the class , there can be only one parameter CLS in parentheses
for static methods, there can be only one parameter, which automatically passes the current class when the value is passed. In the case of Singleton mode.
- Static Methods
- Class @staticmethod trigger class, no parameters () are required in parentheses, and can have multiple parameters
class Plus static method = a function. Creating an object gives you access to the method, as if you were creating a function. A static method is useful if all the programs require object-oriented instead of functions.
Why do you have a class method and a static method?
because without both methods, the object is created at the time of invocation to invoke the method. Both of these methods can be called directly from the class, saving memory.
Class Foo (): def func (self): print "This is common method" @classmethod #类方法 def class_func (CLS) : #参数为cls print "This is class method!!!" @staticmethod #静态方法 def static_method (): print "This is static method!!" obj = foo () obj.func () #用对象直接调用普通方法foo. Class_func () #通过类来直接调用类方法foo. Static_method () #通过类来直接调用静态方法
3. Properties
property to forge a method into a field, an attribute, as a field. Property Instead of a feature
- 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
General method: Foo_obj.func ()
Property method: Foo_obj.prop
Two ways to define a property
- Adorners: Applying adorners on methods
- Static fields are: Static fields that define values as property objects in a class
Adorner mode : Apply the @property adorner to the normal method of the class
-
- Classic class with a @property adorner (as in the previous step)
# ############### Definition ############### class Goods: @property def Price: return "Wupeiqi" # # ############## call ############### obj = Goods () result = Obj.price # automatically executes @property decorated price method and gets the return value of the method
-
- New class with three kinds of @property adorners
# ############### Definition ############### class Goods (object): @property def Price (self): print ' @ Property ' @price. Setter def price (self, value): print ' @price. Setter ' @price. Deleter def Price (self): print ' @price. deleter ' # ############### call ############### obj = Goods () Obj.price # automatically executes the @property decorated price method, and gets the return value of the method Obj.price = 123 # automatically executes the @price. Setter-Modified method, and assigns a value of 123 to the The parameter del Obj.price # of the method automatically executes the @price. Deleter-Modified Price method
Since there are three kinds of access methods in the new class, we can define three methods for the same attribute according to the access characteristics of several properties: Get, modify, delete
Class Goods (object): def__init__ (self): # original price self.original_price = # discount self.discount = 0.8 @property def Price: # Actual prices = Price * Discount new_price = self.original_price * Self.discount return New_price @price. Setter def price (self, value): self.original_price = value @price. deltter def price (self, value): del self.original_priceobj = Goods () obj.price # get commodity prices Obj.price = $ # Modified product Price del obj.price # Delete original price
static fields , creating static fields with values as Property objects
Classic and modern classes are no different when creating properties using static fields
Class Foo: def get_bar (self): return ' Wupeiqi ' bar = property (get_bar) obj = Foo () reuslt = obj. BAR # Automatically calls the Get_bar method and gets the return value of the method print Reuslt
There are four parameters in the property's construction method
- The first parameter is the method name, which
对象.属性 automatically triggers the execution method when called
- The second parameter is the method name, which
对象.属性 = XXX automatically triggers the execution method when called
- The third parameter is the method name, which
del 对象.属性 automatically triggers the execution method when called
- The fourth parameter is a string that
对象.属性.__doc__ is called, and this parameter is the description of the property
Class Foo: def get_bar (self): return ' Wupeiqi ' # * must have two parameters def set_bar (self, value): return ' Set Value ' + Value def del_bar (self): return ' Wupeiqi ' bar = property (Get_bar, Set_bar, Del_bar, ' description ... ') obj = Foo () obj. BAR # Automatically calls the method defined in the first parameter: Get_barobj. BAR = "Alex" # automatically invokes the method defined in the second parameter: Set_bar method, and passes "Alex" as a parameter to del foo.bar # automatically calls the method defined in the third parameter: Del_bar method obj. bae.__doc__ # automatically gets the value set in the fourth parameter: Description ...
Because there are three ways to create properties for static fields, we can define three methods for the same attribute based on the access characteristics of several of their properties: Get, modify, delete
Class Goods (object): def__init__ (self): # original price self.original_price = # discount self.discount = 0.8 def get_price (self): # actual price = Price * Discount new_price = self.original_price * Self.discount return new_price
def Set_price (self, value): Self.original_price = value def del_price (self, value): del Self.original_price Prices = Property (Get_price, Set_price, Del_price, ' price attribute description ... ') obj = Goods () obj. Price # Get commodity prices obj. Price = $ # modifies the original item del obj. Price # Delete original item
Python Object-oriented (ii)