day8-python-Object-Oriented 2

Source: Internet
Author: User

Fields, methods, properties
1 #static fields and normal fields2 classFoo:3     #static fields4Contry ='China'5     def __init__(self,name):6         #normal field7Self.name =name8 9 #calls to normal fields require an object invocation, which requires instantiation of the objectTenHN = Foo ('Henan') One Print(Hn.name) A>>>Henan -HB = Foo ('Hebei') ->>>Hebei the #static fields are called by the class without instantiating the object.Static Field objects and classes in Python can access static fields in the class, and generally do not apply to static fields called by objects
 - Print   >>> China
#补充静态字段, the code is loaded when it is created, and the normal field is invoked when the object is instantiated
#construction methods, static methods, common methods, class methodsclassFoo:#Construction Method    def __init__(self,name): Self.name=name#common method, called by the object, belongs to the class    defShow (self):Print(Self.name)#static methods, which belong to a class, cannot be called by an object, are called with a class    #static methods use static methods when methods in a class do not call an object, as with functions@staticmethoddefF1 (args):Print(args) @classmethoddefF2 (CLS): Ret= CLS ('Alex') ret.show ()#Common methods use object utilityobj = Foo ('Liguangxu') obj.show ()>>>Liguangxu#static methods use class invocationFOO.F1 ('ABC')>>>ABC#Class methods use the class invocation, which by default passes the class name as a parameter, (must have) to initialize the object in the class. foo.f2 ()>>>alex

day8-python-Object-Oriented 2

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.