Python object-oriented | class method classmethod

 

Class: it must by calling the class, but the significance of this method: the variable is inside a class or method to add modifications.

Cases of a store, the audience anniversary discount for each purchase, how to write code for it?

class Goods:
    __discount = 0.8  # 折扣

    def __init__(self,name,origin_price):
        self.name = name
        self.__price = origin_price

    @property
    def price(self):
        return self.__price * Goods.__discount

apple = Goods('apple',5)
banana = Goods('banana',8)
print(apple.price)
print(banana.price)


'''
执行输出:
4.0
6.4
'''

Discount now changed, ending anniversary, restore the original price. How to modify variables __discount it ? So I can not write.

Goods._Goods__discount = 1

How to do it? Define a method, modify the properties

class Goods:
    __discount = 0.8
    def __init__(self,name,origin_price):
        self.name = name
        self.__price = origin_price

    @property
    def price(self):
        return self.__price * Goods.__discount

    def change_discount(self,new_discount):     # 修改折扣
        Goods.__discount = new_discount         #修改了全局的

apple = Goods('apple',5)
banana = Goods(' Banana ' ,. 8 ) apple.change_discount (

 . 1)                         # modify discount. 1 
Print (apple.price)
 Print (banana.price) 

'' ' 
performs an output: 
. 5 
. 8 
' ''

But revised static class variables, does not need to instantiate fishes ah. If you want to change the discount is the whole thing does not involve a specific item and therefore should not use the object to call this method.

 

Class function (@classmethod): i.e. class methods,  and more attention from the class method calls, instead of calling the method in Example

The method does not rely on the object class should be defined as a method, a method class may be arbitrarily class static variable operation

class Goods:
    __discount = 0.8  # 折扣
    def __init__(self,name,origin_price):
        self.name = name
        self.__price = origin_price  # 原价

    @property
    def price(self):  # 价格
        return self.__price * Goods.__discount

    @classmethod
    def change_discount(self,new_discount):
        Goods.__discount = new_discount

Goods.change_discount(1)  #Class class method may be directly invoked by default does not need to pass an object need only pass a parameter can be a parameter class 

Apple = Goods ( ' Apple ' ,. 5 ) 
Banana = Goods ( ' Banana ' ,. 8 ) 

Print (apple.price)
 Print ( banana.price) 

'' ' 
performs an output: 
. 5 
. 8 
' ''

 

Exercises

Suppose I have a class and a class student class, want to achieve as a function of:

  1. Increase the number of classes to perform the operation, to obtain the total number of classes;
  2. Student class inherits from class categories, each instance of a student, class size can be increased;
  3. Finally, I would like to define some of the students, to get the total number of classes.
class C:
    count = 0
    def __init__(self):
        C.cou()

    @classmethod
    def cou(cls):
        cls.count += 1

obj = C()
obj = C()
obj = C()
obj = C()
obj = C()
obj = C()
obj = C()
obj = C()
obj = C()
obj = C()
obj = C()
print(C.count)    #  11
View Code

 

Thoughts: The problem with the class more appropriate way of doing, and why? Because I instantiate the students, but if I get the total number of students in classes from this one instance, the logic is clearly unreasonable. Meanwhile, if you want to get the total number of classes, if you generate a class instance it is not necessary.

class Student:
     __num = 0 

    DEF  __init__ (Self, name, Age): 
        self.name = name 
        self.age = Age 
        Student.addNum ()   # write in __new__ method more appropriate, but have not yet learned, for the time being put here 

    @classmethod 
    DEF addNum (CLS): 
        CLS. __num + =. 1 

    @classmethod 
    DEF getNum (CLS):
         return . CLS __num 


A = Student ( ' Tony ' , 18 is ) 
B = Student ( ' John ', 19)
c = Student('Linda', 20)
print(Student.getNum())         # 3

 

 

Guess you like

Origin www.cnblogs.com/Summer-skr--blog/p/11801434.html