python类继承多态

1、python类如何调用父类构造函数 __init__   (继承过程中强调父类构造方法)

-方法一: super().__init__

class Father():
    def __init__(self):
        print("张三")
class Son(Father):
    def __init__(self):
        super().__init__()
        print("李四")

-方法二: super(自己类名,self).__init__

class Father():
    def __init__(self):
        print("张三")
class Son(Father):
    def __init__(self):
        super(Son,self).__init__()
        print("李四")

-方法三: Father.__init__(self)

class Father():
    def __init__(self):
        print("张三")
class Son(Father):
    def __init__(self):
        Father.__init__(self)
        print("李四")

打印结果:

 2、python类如何调用父类方法   (继承过程中强调父类方法)

super().run()

class Father():
    def run(self):
        print("run")
class Son(Father):
    def run(self):
        #继承父类的方法
        super().run()
        

3、python子类重写父类的方法 (重写也要先继承)

重写:父类提供的方法不能满足子类的需求,需要在子类中定义一个同名的方法,即为重写

重写构造函数  __init__

class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.money = 1000
class Student(Person):
    def __init__(self, name, age, school):
        super().__init__(name, age)
        self.school = school
        print(str(age)+"那年的"+name+self.school)
student=Student("ljf520hj",20,"好想去985")

打印结果:

重写父类的方法,默认搜索原则:先找到当前类,再去找父类

class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.money = 1000
    def eat(self,food):
        print("人吃"+food)
class Student(Person):
    def __init__(self, name, age, school):
        super().__init__(name, age)
        self.school = school
        # print(str(age)+"那年的"+name+self.school)
    def eat(self,food,fruit):
        super().eat(food)
        print("学生"+food+fruit)
student=Student("ljf520hj",20,"好想去985")
student.eat("麻辣烫","水龙果")

打印结果:

4、python私有属性读写

class Student(object):
    def __init__(self,age):
        self.__age=age
    @property
    def age(self):
        print("读年龄",self.__age)
    @age.setter
    def age(self,age):
        self.__age=age
        print("写年龄",self.__age)
s= Student(1)
s.age=10
print(s.age)

5、继承--类方法继承

class Fruit:
    color = '绿色'
    def harvest(self, color):
        print(f"水果是:{color}的!")
        print("水果已经收获...")
        print(f"水果原来是{Fruit.color}的!")
class Apple(Fruit):
    color = "红色"
    def __init__(self):
        print("我是苹果")
a=Apple()
a.harvest("橙色")

打印结果:

5、继承-构造函数的继承

class Father(object):
    def __init__(self, name):
        self.name = name
        print("name: %s" % (self.name))
    def getName(self):
        return 'Father ' + self.name

class Son(Father):
    def getName(self):
        return 'Son ' + self.name

son=Son("ljf520hj")
print(son.getName())

打印结果:

6、封装,继承,多态    先有封装,之后有继承,最后才是多态

class Person(object):
    def __init__(self,name):
        self.name = name
    def feed_pet(self,pet):
        if isinstance(pet,Pet):
           print("pet.name:"+str(pet.nickname)+"sd"+str(pet.role),"self.name"+self.name)
        else:
            print(pet.role,"不是宠物的类型")
class Pet(object):
    role = 'Pet'
    def __init__(self,nickname,age):
        self.nickname = nickname
        self.age = age
    def show(self):
        print(self.nickname,self.age)
class Cat(Pet):
    role=""
    def __init__(self,nickname,age):
        super().__init__(nickname,age)
        print("xsss")
class Dog(Pet):
    role=""
class Tiger(object):
    role="老虎"
cat = Cat("花花",1)
tiger = Tiger()
person=Person("张三")
person.feed_pet(cat)
print("------------------------")
person.feed_pet(tiger)

上班求生存,下班求发展
原文地址:https://www.cnblogs.com/ljf520hj/p/14754843.html