Python Object-oriented advanced

Source: Internet
Author: User

Class Members:
#字段
Normal fields belong to object execution only through object access
A static field belongs to class execution that can be accessed either through an object or through a class
classFoo:def __init__(self,name):#FieldSelf.name =name#Method    defShow (self):Print(self.name) obj= Foo ('Alex')Print(Obj.name)#call field, no parentheses are needed behindObj.show ()#call method, parentheses are required behind

Field instance:

classProvince:#static fields (analogy global variables)Country ='China'    def __init__(self,name):#normal fieldSelf.name =Namehenan= Province ('Henan') Hebei= Province ('Hebei')Print(Henan.name)#output Result: Henan #通过对象访问Print(Province.country)#output: China #通过类访问Print(Henan.country)#output: China #通过对象访问
  #方法
The normal method is saved in the class, and the object calls the argument self, which refers to the =>> call object
Static methods are saved in the class, called directly by the class, and do not require the self parameter
The class method is saved in the class, called directly by the class, and the parameter CLS refers to the =>> current class
Static MethodsclassFoo:defBar (self):Print('Bar') @staticmethoddefSTA ():Print('123') @staticmethoddefstat (A1,A2):Print(A1,A2) Foo.sta () Foo.stat (The)#Output Result:#123#1 2
# class method
class Foo:     def Bar (self):         Print ('bar')    @classmethod    def  Class_f (CLS):         Print(' my own class name ') foo.class_f ()  # access by direct invocation of class # output Result: My own class name
#  The definition format of the property method, the way the field is referenced
class foo:    @property    def  Pro (self):        return 1= Foo () Print (Obj.pro) # output results: 1 The Sett property implements various functions through the adorner . del
# example: Enter page numbers to display paging practice purpose: By using attributes to reduce the last call of code, parentheses.


# The second way to express a property



Python Object-oriented advanced

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.