Object-oriented knowledge and common operation (II.)

Source: Internet
Author: User

Tag: Property parameter size Bracket SEL Pre Member Count method

This article details the members of the Python class, the member modifiers, and the special members of the class .

The members of a class are divided into fields, methods, and properties

Fields: Divided into normal fields and static fields, normal fields are saved in memory for each object, and static fields only hold one copy in the memory of the class.

Methods: Divided into common methods, class methods and static methods,

    • Normal method: Called by the object ; at least one self parameter; When the normal method is executed, the object that invokes the method is automatically assigned the value to auto;
    • Class method: Called by the class ; at least one cls parameter; When the class method is executed, the class that invokes the method is automatically copied to the CLS;
    • Static methods: Called by the class , no default parameters;

Common use of fields and methods is shown in the following example:

1 #the members of a class are divided into fields, methods, and properties2 #fields, divided into normal fields and static fields, ordinary fields belong to objects, static fields belong to class3 #method: Divided into common method, static method and class method4 #Common method: Called by an object, at least one self parameter, when executing the normal method, automatically assigns the object of the method call to the5 #class method: Called by a class, at least another CLS parameter, when the class method is executed, the class that invokes the method is automatically copied to the CLS6 #static method: Called by class with no default parameters7 classFOO:8Func="I am a static field"9     def __init__(self,name):TenSelf.name=name#normal field One  A     defOrd_fun (self): -         Print("normal method, with at least one self field", Self.name) - @classmethod the     defClass_fun (CLS): -         Print("class method, with at least one CLS parameter") - @staticmethod -     defStatic_fun (): +         Print("static methods, no parameters") -  + @property A     defprop (self): at         Print("Property Call") -  - #calls to normal fields: -F=foo ("Eric") - Print(F.name) - #calls to static fields in Print(Foo.func) -  to #invocation of a common method + F.ord_fun () - #invocation of a class method the Foo.class_fun () * #invocation of a static method $ Foo.static_fun ()Panax Notoginseng #property Invocation, consistent with the invocation of a normal method, with only one self argument defined, without parentheses when called - #You can create an illusion that the field is fully consistent when accessing the property the #attributes are created by method variants, and without attributes, the method can be completely substituted +F.prop

Property

1, the basic use of attributes

    • 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: Foo_obj.func ()
    • Property: Foo_obj.prop
    • The function of the Python property: A series of logical operations inside the property, which eventually returns the result of the calculation
1 classPager (object):2     def __init__(self,current_page):3         #page number of the user's current request4Self.current_page=Current_page5         #the number of bars displayed by default per page6self.per_items=107 8 @property9     defStart (self):TenVal= (self.current_page-1) *self.per_items+1 One         returnVal A @property -     defEnd (self): -val=self.current_page*Self.per_items the         returnVal - # -P=pager (2) - Print("second page start:", P.start) + Print("End of second page:", P.end)

2. Two ways to define attributes:

1, adorners, that is, the use of adorners on the method

2, static field, that is, in the class to define the value of the property of the field

Adorner: Classic class: As shown in the example above, the method of defining attributes @property

New class: Properties of the new class are richer than those of the classic class

1 #new class with three kinds of @property adorners2 classGoods (object):3     def __init__(self,name):4Self.name=name5         #Original Price6self.original_price=1007         #Discounted price8self.discount=0.89 Ten @property One     defPrice (self): A         #actual Price -new_price=self.original_price*Self.discount -         returnNew_price the  - @price. Setter -     defPrice (self,value): -         #revise the original price of the product +Self.original_price=value -  + @price. Deleter A     defPrice (self): at         #Delete Item Price -         delSelf.original_price -  -Obj=goods ("Apple") - Print(Obj.price) -obj.price=200 in Print(Obj.price) - delObj.price

static field mode: when creating an attribute with a static field, there is no difference between the classic class and the modern class

Static field way to create a property has three access methods, we can according to their characteristics of the access of several properties, respectively, three methods are defined as the same property: Get, modify, delete

1 classGoods (object):2 3     def __init__(self):4         #Original Price5Self.original_price = 1006         #Discount7Self.discount = 0.88 9     defGet_price (self):Ten         #actual rate = Original Price * Discount OneNew_price = Self.original_price *Self.discount A         returnNew_price -  -     defSet_price (self, value): theSelf.original_price =value -  -     defDel_price (self, value): -         delSelf.original_price +  -Price = Property (Get_price, Set_price, Del_price,'Price Attribute Description ...') +  Aobj =Goods () atObj. Price#Get product price -Obj. Price = 200#revise the original price of the product - delObj. Price#Delete Item Price

Second, modifier of class member

Class members are divided into: Public members: can be anywhere

Private Member: Only accessible within the class, the first two characters are underlined when a private member is named, except for special members (__INIT__) __name

1 #Public members: accessible from anywhere. Static: Objects of a class can be accessed, classes can be accessed internally, and derived classes can access2 #Private member: Accessible only within the class, accessible only within the class3 classC (object):4Name="public static fields"5     __na="private static fields"6     def __init__(self):7self.foo="Public common fields"8Self.__fox="Private Normal field"9 Ten     defFunc (self):#Class internal Access One         Print(Self.foo) A         Print(self.)__fox) -         Print(c.name) -         Print(C.__na) the classD (C):#only public static fields and public common fields can be accessed -     defShow (self): -         Print(c.name) -         #print (C.__na) does not have normal access +         Print(Self.foo) -         #print (Self.__fox) does not have normal access to private fields + Print("class Access:", C.name)#class Access A #Print ("Class access private field:", C.__na) #报错, cannot access atObj2=C () -Obj2.func ()#all can be accessed normally, print four -  -  -Obj_d=D () -Obj_d.show ()

Class of special members in the actual use of know, so do not enumerate

Object-oriented knowledge and common operations (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.