继承的特点在Java这一块有所接触
首先所了解的是所谓继承,就是子类继承了父类的所有的属性和特征
一、一个子类继承父类的简单例子
1.首先建立一个父类
class Father():
def __init__(self):
self.num=1
def __str__(self):
return f'这是建立了一个父类'
Big_Father=Father()
print(Big_Father)
2.接着建立一个子类进行继承
注意,类方法一般都会对自身进行调用,所以每个括号中要添加self
class Son(Father):
pass
small_son=Son()
print(small_son.num)
super函数进行快捷继承
super(类名称,self)
3.多继承
除此之外,我们可能不仅继承一个师傅的衣钵,所以我们常常需要向不同的师傅学习
也就是一个徒弟常常需要继承多个师傅的特点,而一般情况下,会默认使用第一个师傅的各种属性
class Father(object):
def __init__(self):
self.num=1
def printNow_attribute(self):
print(f'我的代号是{self.num}')
def __str__(self):
return f'这是建立了一个父类'
Big_Father=Father()
print(Big_Father)
class Teacher_1(object):
def __init__(self):
self.num = 2
self.learn='语文'
def printNow_attribute(self):
print(f'我的代号是{self.num}')
def __str__(self):
return f'这是建立了另外一个父类'
class Son(Father,Teacher_1):
pass
small_son=Son()
small_son.printNow_attribute()
这是多继承的一个例子
4.重写
值得注意的是,如果父类和子类具有相同的类特征,则以子类作为优先标准。
5.子类对父类的精准调用
子类对父类进行调用,可以在子类中建立函数,再分别导入父类的初始化和引用函数。导入初始化的原因是属性在初始化位置,这个时候调用的参数会覆盖原来的属性。接下来导入函数的作用是调用原来父类的类函数
class Son(Father,Teacher_1):
def __init__(self):
self.num = 3
self.learn = '英语'
def printNow_attribute(self):
print(f'我的代号是{self.num}')
def Father_printNow_attribute(self):
Father.__init__(self)
Father.printNow_attribute(self)
def Teacher_1_printNow_attribute(self):
Teacher_1.__init__(self)
Teacher_1.printNow_attribute(self)
def __str__(self):
return f'这是建立了另外一个子类'
small_son=Son()
print(small_son)
small_son.Father_printNow_attribute()
5.多层继承
类似于for循环可以写多层,
继承之间也有递进关系
super函数进行快捷继承时,如果省略继承的是哪个父类,那么会继承默认值,自己的第一层父亲,而不是父亲的父亲。
6. 私有属性
建立私有属性,子类无法继承。双下划线加名称
建立私有函数,子类无法继承。双下划线加函数名
如下所示,
class Teacher_1(object):
def __init__(self):
self.num = 2
self.learn='语文'
self.__money = 1000
def printNow_attribute(self):
print(f'我的代号是{self.num}')
def __str__(self):
return f'这是建立了另外一个父类'
class Son(Teacher_1):
# def __init__(self):
# self.num = 3
# self.learn = '英语'
def printNow_attribute(self):
print(f'我的代号是{self.num}')
# def Father_printNow_attribute(self):
# Father.__init__(self)
# Father.printNow_attribute(self)
def Teacher_1_printNow_attribute(self):
Teacher_1.__init__(self)
Teacher_1.printNow_attribute(self)
def __str__(self):
return f'这是建立了另外一个子类'
small_son=Son()
print(small_son)
print(small_son.learn)
print(small_son.money)
有属性建立之后不能直接调用,但是可以通过自己设置调用函数调用和修改
当然需要在父类中进行调用和修改,也就是说,一般私有属性的设立和修改调用函数是一起出现的。
父子两类都可以调用这个函数。值得注意的是,修改函数没有调用之前是不能修改的。
class Teacher_1(object):
def __init__(self):
self.num = 2
self.learn='语文'
self.__money = 1000
def printNow_attribute(self):
print(f'我的代号是{self.num}')
def __str__(self):
return f'这是建立了另外一个父类'
def get_money(self):
return self.__money
def set_money(self):
self.__money=500
class Son(Teacher_1):
# def __init__(self):
# self.num = 3
# self.learn = '英语'
def printNow_attribute(self):
print(f'我的代号是{self.num}')
# def Father_printNow_attribute(self):
# Father.__init__(self)
# Father.printNow_attribute(self)
def Teacher_1_printNow_attribute(self):
Teacher_1.__init__(self)
Teacher_1.printNow_attribute(self)
def __str__(self):
return f'这是建立了另外一个子类'
small_son=Son()
tall_teacher=Teacher_1()
print(small_son)
print(small_son.learn)
print(small_son.get_money())
small_son.set_money()
print(small_son.get_money())