Object Oriented (ii)

Source: Internet
Author: User

Members of the Class 1. Fields

Static fields and Normal fields

# class foo:#     country = ' China '            # static field, belongs to class, code is loaded when you have created #     def __init__ (self,name): #         Self.name = name       # Normal field, belonging to the # Ordinary Field object can only be accessed with the object # static field (try to avoid using objects) HN = foo (' henan ') HB = foo (' Hebei ') print (foo.country) In-Memory storage: Static fields save only one copy of the normal field in memory a static field scenario is required for each object: When you create an object from a class, you use a static field if each object has the same field
2. Methods
1. Common method: At least one self, the object invokes execution
2. Static method: Arbitrary parameter, class call execution
3. Class method : At least one CLS, class call execution
For all methods, all belong to the class, so only one copy is stored in memory
Class Foo:    def __init__ (self,name):        self.name  = name# Normal method, object invocation execution, belongs to class    def num (self):        print ( Self.name) #把不需要通过对象调用执行的普通方法做一些改动, becomes a static method (the normal function in Python) and executes    @staticmethod    def Show (ARG) by invoking the class directly:        Print (arg, ' static method ') #类方法, no arguments required,    @classmethod    def clas (CLS):        a = CLS (' a ')        a.num ()        print (CLS) Foo.show (' This is ') obj = Foo (' This is the normal method ') Obj.num () Foo.clas ()
3. Properties



Object Oriented (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.