Object-oriented Advanced

Source: Internet
Author: User
Object- oriented advanced Syntax section

The method of decorating is changed into a static method by @staticmethod Adorner, what is static method? In fact, it is not difficult to understand, the ordinary method can be called directly after instantiation, And in the method can pass self. Invokes an instance variable or a class variable, but a static method cannot access an instance variable or a class variable, and a method that cannot access an instance variable or a class variable is, in fact, nothing to do with the class itself, and its only association with a class is the need to call this method through the class name.

class schoolmember (object): Def __init__ (self,name,age,sex): Self.name = NA  Me self.age = age Self.sex = Sex Member_nums = 0def introduce (self): print ("My name is%s,and I am %s year old. "% (Self.name,self.age)) @staticmethod def talk (): Print (" I like to study Python ") Class Teacher (        Schoolmember): Def __init__ (self,name,age,sex,course,salary): Super (Teacher,self). __init__ (Name,age,sex) Self.course = Course Self.salary = Salary def teaching (self): print ("Teacher%s is teaching%s."% (SELF.N Ame,self.course) S1 = Teacher ("Alex", "a", "femal", "Python", 10000) print ("Before:", S1.member_nums) Schoolmember.member_nums = 12print ("Before:", s1.member_nums) s1.member_nums = 666 #是在类中重新生成一个变量print ("After:", s1.member_nums) Schoolmember.member_nums = 12print ("After:", S1.member_nums) 

In the above code, Member_nums is a class variable, if you call S1.member_nums directly, call is the value inside the class, if s1.member_nums = 666, equals to add a new variable in the instance, this time, when the value of the class is modified, Does not affect the value of the variable within the instance. The output of the above code is as follows:

before:0
Before:12
after:666
after:666

The static method of the @staticmethon class is:

Schoolmember (= =  %  %= schoolmember (,,

In the above code, if there is no @staticmethon, the code execution is certainly not a problem, but when the @staticmethod is available, the system prompts for a missing parameter. If we turn a method into a static method, then this method does not have much to do with the instance.

Class Schoolmember (object):    def __init__ (self,name,age,sex):        self.name = name        self.age =        Age Self.sex = sex    member_nums = 0def Introduce (self):        print ("My name was%s,and I am%s year old."% (Self.name,self.ag e))    @classmethod        #类方法, cannot access instance variable    def talk (self):        print ('%s like to study Python '  % schoolmember.member_nums)    @staticmethod        #让方法在类中剥离, not related to class, call to pass parameter    def walk (self):        print ("%s Is walking ... "%self) #SchoolMember. Talk ()    #不能调用, class is no way to access instance variables, only access yourself S1 = Schoolmember (" Alex "," a "," Female ")  #实例化s1. Walk ("Alex")

@staticmethod static method is to make a method in a class not associated with a class, and to pass a parameter to invoke when it is called.

Class method

Class methods pass through the @classmethod adorner implementation, the difference between a class method and a common method is that class methods can only access class variables and cannot access instance variables

Class Schoolmember (object):    def __init__ (self,name,age,sex):        self.name = name        self.age =        Age Self.sex = sex    member_nums = 0def Introduce (self):        print ("My name was%s,and I am%s year old."% (Self.name,self.ag e)    # @classmethod        #类方法, cannot access instance variable    def talk (self):        print ('%s like to study Python '  %self.name) Schoolmember.member_nums#schoolmember.talk ()    #不能调用, classes are no way to access instance variables, only access their own S1 = Schoolmember ("Alex", "a", "Female")  #实例化s1. Talk ()

In the above code, the(1) class is not directly accessible to the properties of the instance , and (2) the function of the@classmethod is to allow the program to access only the variables in the class, such as the Schoolmember.member_ in the code above. Nums, this is a class method that we can access in talk, but cannot access self.name because @classmethod can only access class properties.

Class Schoolmember (object):    def __init__ (self,name,age,sex):        self.name = name        self.age =        Age Self.sex = sex    member_nums = 0def Introduce (self):        print ("My name was%s,and I am%s year old."% (Self.name,self.ag e))    @classmethod        #类方法, cannot access instance variable    def talk (self):        print ('%s like to study Python '  %self.name) Schoolmember.member_nums#schoolmember.talk ()    #不能调用, classes are no way to access instance variables, only access their own S1 = Schoolmember ("Alex", "a", "Female")  #实例化s1. Talk () The results are as follows: Traceback (most recent call last):  File "/home/zhuzhu/day7/staticmethon method. Py", line +, in <module>s1.talk ()  file "/home/zhuzhu/day7/ Staticmethon method. py ", line-in-talk-    print ("%s like to study Python "  %self.name) Attributeerror:type object ' Sc Hoolmember ' has no attribute ' name '

As can be seen above, the code above @classmethon prohibits instance variables in the class, and can only use class variables. That is, you cannot use Self.name, Self.age, and self.sex, only variables of the self.nember_nums and Schoolmember.member_nums classes. As follows:

Class Schoolmember (object):    def __init__ (self,name,age,sex):        self.name = name        self.age =        Age Self.sex = sex    member_nums = 0def Introduce (self):        print ("My name was%s,and I am%s year old."% (Self.name,self.ag e))    @classmethod        #类方法, cannot access instance variable    def talk (self):        print ('%s like to study Python '  % schoolmember.member_nums) Schoolmember.member_nums#schoolmember.talk ()    #不能调用, the class is not able to access instance variables, only access itself S1 = Schoolmember ("Alex", "Female")  #实例化s1. Talk () The results are as follows: 0 like to study python

Property method

The function of a property method is to turn a method into a static property by @property

Schoolmember (= =  %  %= schoolmember (,,

If you do not add @property, the program is able to run normally, but after the addition of @property, the program run error, what is the reason? Because @property is the class's method into the class's properties, when called we only need to execute s1.walk () do not need to add parentheses to execute, as follows:

Schoolmember (= =  %  %= schoolmember (,,

In the above code, the @property is to turn the method of the class into a member property, which we can invoke directly using S1.walk.

Classic class vs new Class

Class A:             #经典类的写法, the new class is a (object) as little as possible with the classic class, all with the new class now    def __init__ (self,name):        self.name = name    def f1 (self ):        print ("F1, Base") class B (A):    def __init__ (self,name):        super (B,self). __init__ (name)    # def F1 (self):    #     Print ("F1, Come On") class C (A):    def __init__ (self,name):        super (C,self). __init__ (name)    #def F1 (self):        #print ("F1, get together!" ") class D (b,c):    PASSD = D (" Alex ") d.f1 ()

In the above code, Class D inherits Class B and Class C, when we execute the method in Class D, the first is to find in Class B, the classic class and the new class are the same, if not found, the classic class is to go to a class to find, and the new class is to go to the class C, the example is as follows: (note: Must go to 2 x version of the operation is only a difference, 3.X optimization, Class B found in the C class to find it)

Here is the order of execution for classic and modern classes:

(New Class) executes class of peers first

(2) Classic Class (first class is performed)

Related 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.