The path to Python-Object oriented (Advanced article)

Source: Internet
Author: User

Object-oriented Advanced: class members, modifiers for class members, special members of classesclass member

Class members fall into three main categories: Fields, Methods, properties

First, the field

    • static field (belongs to Class)
    • Normal field (belongs to object)

1 classCity :2     #static fields3Country ="China"4 5     def  __init__(self,city_name):6         #normal field7Self.city_name =City_name8 9     defShow (self):Ten         Print(Self.city_name) One  A  -Obj1 = City ("Harbin") -Obj2 = City ("Shanghai") the #common Field object to access - Print(Obj1.city_name,obj2.city_name) - #static fields are accessed through the class name - Print(City.country)
Field

static field definition and application: As the program executes, it disappears as the program ends, so we call it a static field when the program ' survives ' the field. It is like a global variable that does not belong to any object, we can call it directly using the class, or we can use it when the object uses the method. It is an object-shared variable that exists in the memory of the class.

Special: objects can also access static fields.

Follow the rules: Normal fields can only be accessed by objects, and static fields are accessed with classes. (with the last resort to access the object)

Second, the method

    • Static methods
    • Class method
    • Common methods

The path to Python-Object oriented (Advanced article)

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.