python_ static portfolio, inheritance

Static Property: The method can be used in the function call data attributes defined in the attribute class

Add in front of the property to be modified function @property

class Tue ():
     DEF  the __init__ (Self, name, Age, Data): 
        the self.name = name 
        self.age = Age 
        self.data = Data 
    @Property 
    DEF info_self (Self):
         return the self.name 
R1 = Tue ( ' ALOM ' , 14, " Zhejiang " )
 Print (r1.info_self)
 # output: 
# ALOM 
# necessary to add parentheses can be called directly
Static properties

Method class: class calls the function attributes directly, not necessary to generate examples

class Tue (): 
    Tage =. 1
     DEF  the __init__ (Self, name, Age, Data): 
        the self.name = name 
        self.age = Age 
        self.data = Data 
        self.age =. 1 
    @classmethod 
    DEF info (CLS):
         Print ( ' from info ' , cls.tage) 
Tue.info () 
# necessary to generate examples herein can call the function attribute class method 
# If no error will be added classmethod
Class Methods

Static method: nominally classified management, can not be generated instance attribute class in the calling function, a tool package belonging to the class

To add the modified front @staticmethod

class Tue():
    tage = 1
    def __init__(self,name,age,data):
        self.name = name
        self.age = age
        self.data = data
        self.age = 1
    @staticmethod
    def text(x,y):
        print(x,y)
Tue.text(1,2)
print(Tue.__dict__)
r1  = Tue('name',14,'dad')
print(r1.__dict__)
Static method

Inheritance: subclass inherits the parent class, met with the same attributes as the parent class when the subclass inherits the parent class, will create in their own internal do not go to modify the properties of the parent class

class Dad:
    momy = 10
    def __init__(self,name):
        self.name = name

    def hit_son(self):
        print("打了一下son")
class Son(Dad):
    momy = 10000000000
r1 = Son('alex')
print(r1.momy)
print(Dad.__dict__)
print(Son.__dict__)
inherit
Code
Link: https: //pan.baidu.com/s/1V4PT_0W7tvyF_IyCq-TF4A
extraction code: qrhz

Guess you like

Origin www.cnblogs.com/Alom/p/12109325.html