方法的重写,覆盖 override

  

复制代码
class A:
    def __init__(self,a):
        self.a=a
class B(A):
    def __init__(self,b,c):
        self.b=b
        self.c=c

    def printv(self):
        print(self.b)
        print(self.a)

f=B(200,300)
print(f.__dict__)
print(f.__class__.__bases__)
f.printv()
复制代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A:
    def __init__(self,a,d=10):
        self.a=a
        self.__d=d
 
class B(A):
    def __init__(self,b,c):
        A.__init__(self,b+c,b-c)
        self.b=b
        self.c=c
    def printv(self):
        print(self.b)
        print(self.a)
 
f=B(200,300)
print(f.__dict__)
print(f.__class__.__bases__)
f.printv()

  

如果父类中定义了__init__方法,你就该在子类的__init__中调用它,那子类什么时候自动调用父类的__init__方法呢

 

复制代码
class A:
    def __init__(self):
        self.a1='a1'
        self.__a2='a2'
        print('A init')
class B(A):
    pass

b=B()
print(b.__dict__)
复制代码

B实例的初始化会自动调用基类A的__init__方法

复制代码
class A:
    def __init__(self):
        self.a1='a1'
        self.__a2='a2'
        print('A init')
class B(A):
    def __init__(self):
        A.__init__(self)
        self.b1='b1'
        print('B init')
b=B()
print(b.__dict__)
复制代码

 

复制代码
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)
c.show()
复制代码

不调用父类__init__方法的,会导致没有实现继承效果,所以在子类的__init__方法中,应该显式调用父类的__init__方法

 

复制代码
class Animal:
    def __init__(self,age):
        self.age=age
        print('Animal init')
    def show(self):
        print(self.age)

class Cat(Animal):
    def __init__(self,age,weight):
        print('Cat init')
        # 调用父类__init__方法的顺序决定这show方法的结果
        super().__init__(age)
        self.age=age+2
        self.weight=weight
        # Animal.__init__(self,age)

c=Cat(10,5)
c.show()
复制代码

 

 

 上图中没有调用父类__init__方法,__dict__没有_Animal__age属性,打印Cat类属性

 

 上图中调用父类__init__方法,Cat子类生成的object的__dict__有_Animal__age属性,优先调用对象自身属性

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Animal:
    def __init__(self,age):
        self.__age=age
        print('Animal init')
    def show(self):
        print(self,self.__age) # 被python替换成self._Animal__age
 
class Cat(Animal):
    _Animal__age=888
    def __init__(self,age,weight):
        super().__init__(age)
        print('Cat init')
        self.__age=age+1
        self.__weight=weight+1
 
c=Cat(10,5)
c.show()
print(c.__dict__)

  

复制代码
class Animal:
    def __init__(self,age):
        self.__age=age
        print('Animal init')
    def show(self):
        print(self,self.__age) # 被python替换成self._Animal__age

class Cat(Animal):
    _Animal__age=888
    def __init__(self,age,weight):
        super().__init__(age)
        print('Cat init')
        self.__age=age+1
        self.__weight=weight+1
    def show(self): # 调用私有属性,应该override父类相关方法
        print(self,self.__age)

c=Cat(10,5)
c.show()
print(c.__dict__)
复制代码

一个原则,自己的私有属性,就该用自己的方法读取和修改,不要借助其他类的方法,即使是父类或者派生类的方法

posted @   ascertain  阅读(193)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示