Inheritance and polymorphism of python classes and built-in functions of classes

Inheritance and polymorphism of python classes
  • Functionality of primitive types obtained through inheritance
  • What is inherited is called the base class or parent class, and what is inherited is called the subclass
  • The subclass (inheritor) has all the methods and class attributes of the parent class (inherited)
  • The parent class (inherited) does not have the properties and methods of the subclass (inheritor)
  • If the subclass inherits the method of the parent class and the subclass also has the same method, then the method of the subclass takes effect, and you can use this to rewrite the inherited method ( polymorphism of the class )

示例

class Parent:#默认继承object类
    name = "father"
    age = 56
    moneys = "1000w"

    def __init__(self) -> None:
        pass
        # self.name = "father"
        # self.age = 56
        # self.moneys = "1000w"

    def behaver(self):
        print("{}老了走不动了".format(self.age))

    def money(self):
        print("我继承了爸爸的money{}".format(self.moneys))


class children(Parent):#继承父类,参数可以是多个父类  就是多重继承
    def __init__(self) -> None:
        self.name = "child"
        self.age = 26

    def behaver(self):
        print("{}年轻健步如飞".format(self.age))

    def money2(self):
        print("我没有money,")


# parent1 = Parent()
# parent1.behaver()

child1 = children()#实例化child类
child1.money() #继承了父类的money

#类的多态
child1.behaver()#26年轻健步如飞   因为方法名和属性名一致 所以重新了父类的方法
super() function
  • Execute the constructor of the parent class in the constructor of the subclass and pass parameters
  • super (subclass name, self). The method of the parent class, and can pass parameters
class Parent:
    def __init__(self, money) -> None:
        self.name = "father2"
        self.age = 54
        self.moneys = money

    def money(self):
        print("我继承了爸爸的money{}".format(self.moneys))


class children(Parent):
    def __init__(self) -> None:
        super(children, self).__init__("5000w")  # 实例化的时候执行父类的构造函数
        self.name = "child"
        self.age = 26

    def money2(self):
        print("我没有money,")


child1 = children()
child1.money()  # 继承了爸爸的money5000w
higher-order functions in a class
Function name usage return value
_str_() Generally return the description information of the class (examples are as follows) a piece of descriptive information
_getattr_(self,key) Key calls any non-existing attribute name (key) and will return a prompt message Can be informational or not returned
_setattr_(self,key,value) Intercept attributes and values ​​​​that do not exist in the current class, and some processing can be performed dispensable
_call_ Passing parameters to an instance object of a class will trigger dispensable
  • __str__
class text:
    def __str__(self) -> str:
        return "这是一个测试类"
print(text()) #这是一个测试类
  • __getattr__
class text2:
    def __getattr__(self, key) -> Any:
        print("当前的这个变量:{}不存在".format(__name))
a = text2()
a.rte  #当前的这个变量:rte不存在
  • __setattr__
class text3:
    def __init__(self, name) -> None:
        self.name = name
#拦截当前类中不存在的属性与值,如果不存在  就添加
    def __setattr__(self, key: str, value: Any) -> None:
        if key not in self.__dict__:
            self.__dict__[key] = value

t3 = text3("wsm")
t3.age = "123"
print(t3.name)#wsm
print(t3.age)#123
  • __call__
class text:
    def __call__(self, **kwargs) -> None:
        print("这是一个__call__", kwargs)

t = text()
t(name="dweiwei")#这是一个__call__ {'name': 'dweiwei'}

Guess you like

Origin blog.csdn.net/qq_51075057/article/details/130691427