类的继承

1.1

class Animal:
    def __init__(self,name):
        self._name = name
    def shout(self):
        print('{} shouts'.format(self.__class__.__name__))
    @property
    def name(self):
        return self._name
class Cat(Animal):
    pass
cat = Cat('garfield')
cat.shout()
cat.name
#通过继承,猫类不用写代码,直接继承父类的属性和方法
#Cat类没有定义init初始化函数,所有的都继承自Animal

1.1.1

class A:
    def __init__(self):
        self.a1 = 'a1'
        self.__a2 = 'a2'
        print('A init')
class B(A):
    def __init__(self):
        self.b1 = 'b1'
        print('B init')
b = B() #A init
b.__dict__ #{'b1': 'b1'}
#当子类有自己的init函数时,B类的实例b的字典就只有自己传入的参数,参照1.5父类和子类都有init函数,子类需要调用父类的init函数

1.2

class Animal:
    __COUNT = 100
    HEIGHT = 0
    def __init__(self,age,weight,height):
        self.__COUNT += 1
        self.age = age
        self.__weight = weight
        self.HEIGHT = height
    def eat(self):
        print('{} eat'.format(self.__class__.__name__))
    def __getweight(self):
        print(self.__weight)
    @classmethod
    def showcount1(cls):
        print(cls.__COUNT)
    @classmethod
    def __showcount2(cls):
        print(cls.__COUNT)
    def showcount3(self):
        print(self.__COUNT)
class Cat(Animal):
    NAME = 'CAT'
    __COUNT = 200
c = Cat(3,5,15)
c._Animal__getweight()
Cat.__dict__

1.3

class Animal:
    def shout(self):
        print('Animal shouts')
class Cat(Animal):
    def shout(self):
        print('miao')
    def shout(self):
#         print(super())
#         print(super(Cat,self))
        super().shout()
        super(Cat,self).shout()
        self.__class__.__base__.shout(self)
c = Cat()
c.shout()
class Animal:
    def shout(self):
        print('Animal shouts')
class Cat(Animal):
    def shout(self):
        print('miao')
    def shout(self):
#         print(super())
#         print(super(Cat,self))
        super().shout()
        super(Cat,self).shout()
        self.__class__.__base__.shout(self)
c = Cat()
c.shout()

1.4

class Animal:
    @classmethod
    def class_method(cls):
        print('class_method_animal')
    @staticmethod
    def static_method():
        print('static_method_animal')
class Cat(Animal):
    @classmethod
    def class_method(cls):
        print('class_method_cat')
    @staticmethod
    def static_method():
        print('static_method_cat')
c = Cat()
c.class_method()
c.static_method()
#这些方法都可以覆盖,原理都一样,属性字典的搜索顺序

1.5

class A:
    def __init__(self,a):
        self.a = a
class B(A):
    def __init__(self,b,c):
        A.__init__(self,b+c)
        self.b = b
        self.c = c
    def printv(self):
        print(self.b)
        print(self.a)
#好习惯,父类中定义了init,你就改在子类中的init调用它
f = B(200,300)
f.printv()
f.__dict__
f.__class__.__bases__

1.6

class Animal:
    def __init__(self,age):
        print('animal init')
        self.__age = age
    def show(self):
        print(self.__age)
        
class Cat(Animal):
    def __init__(self,age,weight):
        super().__init__(age)
        print('cat init')
        self.__age = age +1
        self.__weight = weight
c = Cat(10,5)
a = Animal(100)
c.__dict__ #{'_Animal__age': 10, '_Cat__age': 11, '_Cat__weight': 5}
a.__dict__ #{'_Animal__age': 100}
c.show() #10
#自己的私有属性,就该自己的方法修改,和读取,不要借助其他的类和方法

1.7

class Animal:
    def __init__(self,age):
        print('animal init')
        self.age = age
    def show(self):
        print(self.age)
        
class Cat(Animal):
    def __init__(self,age,weight):
#         super().__init__(age)
        print('cat init')
        self.age = age +1
        self.weight = weight
c = Cat(10,5)
a = Animal(100)
c.__dict__ #{'age': 11, 'weight': 5}
a.__dict__ #'age': 100}

 2.1Mixin类

class Document:
    def __init__(self,content):
        self.content = content
    def print(self):
        raise NotImplementedError()
class Word(Document):
    pass
class Pdf(Document):
    pass
#基类提供的方法不应该具体实现,因为它未必适合子类的打印,子类中需要覆盖重写
#print是一种能力=====打印能力不是所有的document的子类都需要,所以从这个角度出发有问题
#2.需要打印的子类加上
#2.1如果在现有的子类直接加上,违反了ocp原则,继承后增加

引出了mixin类

class Document:
    def __init__(self,content):
        self.content = content
class Word(Document):pass
class Pdf(Document):pass

class PrintableMinin:
    def print(self):
        print(self.content)

class PrintableWord(PrintableMinin,Word):pass
print(PrintableWord.__dict__)
print(PrintableWord.__mro__)
p = PrintableWord('abc')
p.print()
使用原则:
mixin类中不应该显示的出现init初始化方法
通常不能独立工作,因为他是准备混入别的类中的
mixin的类的祖先也应该是mixin类
本文为原创文章,转载请标明出处
原文地址:https://www.cnblogs.com/harden13/p/9010757.html