Home  >  Article  >  Backend Development  >  Object-oriented advanced

Object-oriented advanced

巴扎黑
巴扎黑Original
2017-06-23 14:46:191140browse

Object-oriented advanced syntax part

By@staticmethod The decorator can turn the decorated method into a static method. What is a static method? In fact, it is not difficult to understand that ordinary methods can be called directly after instantiation, and instance variables or class variables can be called through self. in the method, but static methods cannot access instance variables or class variables, and one cannot access the instance. The methods of variables and class variables actually have nothing to do with the class itself. Its only connection with the class is that the method needs to be called through the class name.

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 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.name,self.course))



s1 = Teacher("alex",22,"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 s1.member_nums is called directly, the value in the class is called; if s1.member_nums = 666, which is equivalent to adding a new variable to the instance. At this time, when modifying the value of the class, it will not affect the value of the variable in the instance. The output of the above code is as follows:

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

Static method of class @staticmethon:

 SchoolMember(====  %  % %= SchoolMember(,,

In the above code, if there is no @staticmethon, there will definitely be no problem in code execution, but when there is @staticmethod , the system prompts that one parameter is missing. If we turn a method into a static method, then this method has little 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 is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %SchoolMember.member_nums)

    @staticmethod        #让方法在类中剥离,与类没有关系,调用要传递参数
    def walk(self):
        print("%s is walking......" %self)


#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.walk("alex")

@staticmethod The static method is to make the method in the class not related to the class, and it can only be called by passing parameters when calling.

Class method

## Class method passes @classmethod Decorator implementation, the difference between class methods and ordinary methods 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 is %s,and I am %s year old." %(self.name,self.age))

    #@classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %self.name)

SchoolMember.member_nums
#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.talk()

In the above code, (1) Classes cannot directly access the attributes in instances; (2)@classmethod is used to allow the program to only access variables in the class, such as SchoolMember.member_nums in the above code , this is a class method, we can access it in talk, but we cannot access self.name, because @classmethod can only access class attributes.

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 is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %self.name)

SchoolMember.member_nums
#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.talk()

运行结果如下:
Traceback (most recent call last):
  File "/home/zhuzhu/day7/staticmethon方法.py", line 18, in <module>s1.talk()
  File "/home/zhuzhu/day7/staticmethon方法.py", line 13, in talk
    print("%s like to study python"  %self.name)
AttributeError: type object 'SchoolMember' has no attribute 'name'

As can be seen from the above, the above code @classmethon prohibits instance variables in the class. Only class variables can be used. That is, self.name, self.age and self.sex cannot be used, only variables of the self.nember_nums and SchoolMember.member_nums classes can be used. 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 is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %SchoolMember.member_nums)

SchoolMember.member_nums
#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.talk()

运行结果如下:0 like to study python

Attribute method

Attribute method The function is to turn a method into a static property through @property

 SchoolMember(====  %  % %= SchoolMember(,,

If @property is not added, the program can run normally, but @ is added After property, an error occurs when running the program. What is the reason? Because @property turns class methods into class attributes, when calling, we only need to execute s1.walk() without adding parentheses, as follows:

 SchoolMember(====  %  % %= SchoolMember(,,

In the above code, @property turns the class method into a member property. We can call it directly using s1.walk.

Classic vs. New Class

class A:             #经典类的写法,新式类是A(object)尽量少用经典类,都用新式类现在
    def __init__(self,name):
        self.name = name

    def f1(self):
        print("f1,搞基")class B(A):
    def __init__(self,name):
        super(B,self).__init__(name)

    # def f1(self):
    #     print("f1,来呀")class C(A):
    def __init__(self,name):
        super(C,self).__init__(name)

    #def f1(self):
        #print("f1,一起搞!")class D(B,C):
    pass

d = D("Alex")
d.f1()

In the above code, class D inherits class B and class C. When we execute the method in class D, we first search in class B. This classic class and the new class The classes are all the same. If the search cannot be found, the classic class is searched in class A, while the new class is searched in class C. The example is as follows: (Note: The difference must be run in version 2.X. 3 . (New-style class) Execute the class of the same level first

(2) Classic class (execute the previous class first Level class)

The above is the detailed content of Object-oriented advanced. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn