上节课我们学习了如何定义一个类,使用类,这节课我们来学习类的继承吧。
类的继承,就像是一个妈妈和一个孩子,孩子拥有一些妈妈的特性,还有一些自己新的特性。
那我们先来学习类是如何继承的吧!
import os
class cat():
def __init__(self,name,age,weight):
self.__name = name
self.__age = age
self.__weight = weight
def introduce(self):
print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.__name,self.__age,self.__weight))
def say(self):
os.system("say 喵喵喵~喵喵喵~喵喵喵")
def revise(self,name,age,weight):
self.__name = name
self.__age = age
self.__weight = weight
def __plus(self,a,b):
return a + b
def __minus(self,a,b):
return a - b
def __cheng(self,a,b):
return a * b
def __chu(self,a,b):
return a / b
def coculater(self,a,b,c):
if c == "+":
print(self.__plus(a,b))
elif c == "-":
print(self.__minus(a,b))
elif c == "*":
print(self.__cheng(a,b))
else:
print(self.__chu(a,b))
class kitten(cat):
pass
在新定义的kitten类里写上它的父类,就可以完成类的继承了。
小猫咪跟大猫咪的叫声不一样,是喵呜喵呜叫的,我们就应该在kitten类中定义一个say函数来覆盖父类cat里的say函数。
import os
class cat():
def __init__(self,name,age,weight):
self.__name = name
self.__age = age
self.__weight = weight
def introduce(self):
print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.__name,self.__age,self.__weight))
def say(self):
os.system("say 喵喵喵~喵喵喵~喵喵喵")
def revise(self,name,age,weight):
self.__name = name
self.__age = age
self.__weight = weight
def __plus(self,a,b):
return a + b
def __minus(self,a,b):
return a - b
def __cheng(self,a,b):
return a * b
def __chu(self,a,b):
return a / b
def coculater(self,a,b,c):
if c == "+":
print(self.__plus(a,b))
elif c == "-":
print(self.__minus(a,b))
elif c == "*":
print(self.__cheng(a,b))
else:
print(self.__chu(a,b))
class kitten(cat):
def say(self):
os.system("say 喵呜~喵呜~喵呜")
现在,我想定义一个新的introduce函数来介绍它的姓名、年龄、体重。
介绍格式如下:
可爱的小猫咪喵喵5岁了,它重10千克。
那这样,cat类里的私有变量就必须改成公有变量了。
import os
class cat():
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def introduce(self):
print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
def say(self):
os.system("say 喵喵喵~喵喵喵~喵喵喵")
def revise(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def __plus(self,a,b):
return a + b
def __minus(self,a,b):
return a - b
def __cheng(self,a,b):
return a * b
def __chu(self,a,b):
return a / b
def coculater(self,a,b,c):
if c == "+":
print(self.__plus(a,b))
elif c == "-":
print(self.__minus(a,b))
elif c == "*":
print(self.__cheng(a,b))
else:
print(self.__chu(a,b))
class kitten(cat):
def say(self):
os.system("say 喵呜~喵呜~喵呜")
def introduce(self):
print("可爱的小猫咪%s%d岁了,它重%d千克。"%(self.name,self.age,self.weight))
还有一种情况,就是一个类继承了多个类。
比如说下面这样的。
import os
class cat():
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def introduce(self):
print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
def say(self):
os.system("say 喵喵喵~喵喵喵~喵喵喵")
def revise(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def __plus(self,a,b):
return a + b
def __minus(self,a,b):
return a - b
def __cheng(self,a,b):
return a * b
def __chu(self,a,b):
return a / b
def coculater(self,a,b,c):
if c == "+":
print(self.__plus(a,b))
elif c == "-":
print(self.__minus(a,b))
elif c == "*":
print(self.__cheng(a,b))
else:
print(self.__chu(a,b))
class animal():
def __init__(self,a,name,age,weight):
self.animal = a
self.name = b
self.age = age
self.weight = weight
def say_animal(self):
os.system("say 我是一个可爱的动物")
class kitten(cat,animal):
def say(self):
os.system("say 喵呜~喵呜~喵呜")
def introduce(self):
print("可爱的小猫咪%s%d岁了,它重%d千克。"%(self.name,self.age,self.weight))
最后,我们来讲一下多次的继承。
import os
class animal():
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def introduce(self):
print("小动物%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
def say(self):
os.system("say 我是一个可爱的动物")
def revise(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def __plus(self,a,b):
return a + b
def __minus(self,a,b):
return a - b
def __cheng(self,a,b):
return a * b
def __chu(self,a,b):
return a / b
def coculater(self,a,b,c):
if c == "+":
print(self.__plus(a,b))
elif c == "-":
print(self.__minus(a,b))
elif c == "*":
print(self.__cheng(a,b))
else:
print(self.__chu(a,b))
class cat(animal):
def introduce(self):
print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
def say(self):
os.system("say 喵喵喵~喵喵喵~喵喵喵")
class kitten(cat):
def say_hello(self):
os.system("say 我好可爱啊")
def introduce(self):
print("可爱的小猫咪%s%d岁了,它重%d千克。"%(self.name,self.age,self.weight))
这节课我们就讲到这里,下节课我们来讲字典。
下次更新时间6月4日或6月5日。



