Python 从入门到实战24(类的继承)

我们的目标是:通过这一套资料学习下来,通过熟练掌握python基础,然后结合经典实例、实践相结合,使我们完全掌握python,并做到独立完成项目开发的能力。

上篇文章我们讨论了类的定义、使用方法、@property的相关知识。今天我们将学习一下类的继承。

1、继承的基本语法

继承是面向对象编程最重要的特性之一,被继承的类称为父类或者基类。在它的基础上新建的类称为子类或者派生类。

继承的语法格式:

class ClassName(baseclasslist):

'''类的帮助信息'''

statement #类体

参数说明:ClassName -----指定类名

statement -----类体,主要由类变量、方法和属性等定义语句组成

baseclasslist----要继承的基类,可以有多个,类名之间由,分割

1)举例1:

#水果类为基类,然后创建其派生类的练习

class Fruit: #定义水果类

color = "green" #定义类的属性

def harvest(self,color): #定义方法

print("水果颜色是:"+color+"的!")

print("水果收获了!")

print("水果开始是:"+Fruit.color+"的!") #输出类的属性

class Banana(Fruit): #定义派生类香蕉

color = "yellow"

def init(self):

print("我是香蕉")

class Pear(Fruit): #定义派生类梨

color = "cyan"

def init(self):

print("我是梨")

banana = Banana() #创建类的实例

banana.harvest(Banana.color)#调用基类的方法

pear = Pear() #创建类的实例

pear.harvest(Pear.color)#调用基类的方法

输出结果参考:

2)举例2

#举例汽车类

class Vehicle:

def init(self, make, model, year):

self.make = make

self.model = model

self.year = year

def start(self):

print(f"{self.make} {self.model} started.")

def stop(self):

print(f"{self.make} {self.model} stopped.")

class Car(Vehicle):

def start(self):

print(f"{self.make} {self.model} revved the engine and started.")

car = Car()

car.start()

这样写执行会报错,car = Car() 因为car 继承Vehicle类,继承init方法

需要加上参数:car = Car("BYD","宋pro","2025")

改写后正常输出:

2、方法重写

基类的成员都会被派生类继承,当基类中的某个方法不完全适用派生类时,可以在派生类重写类的方法。还是上面的例子,我们重写harvest 方法

举例说明:

#水果类为基类,然后创建其派生类的练习

class Fruit: #定义水果类

color = "green" #定义类的属性

def harvest(self,color): #定义方法

print("水果颜色是:"+color+"的!")

print("水果收获了!")

print("水果开始是:"+Fruit.color+"的!") #输出类的属性

class Banana(Fruit): #定义派生类香蕉

color = "yellow"

def init(self):

print("我是香蕉")

class Pear(Fruit): #定义派生类梨

color = "cyan"

def init(self):

print("我是梨")

def harvest(self,color):
print("梨的颜色是:" + color + "的!")
print("梨收获了!")
print("梨开始是:" + Fruit.color + "的!") # 输出类的属性

banana = Banana() #创建类的实例

banana.harvest(Banana.color)#调用基类的方法

pear = Pear() #创建类的实例

pear.harvest(Pear.color)#调用基类的方法

上面代码黄色阴影的部分是重写方法,输出结果参考:

3、派生类中调用基类的__init__()方法

在派生类中定义__init__()方法时,不会自动调用基类的__init__()。

如下面例子:

#水果类为基类,然后创建其派生类的练习

class Fruit: #定义水果类

def init(self,color="green"):

Fruit.color = color

def harvest(self): #定义方法

print("水果开始是:"+Fruit.color+"的!") #输出类的属性

class Banana(Fruit): #定义派生类香蕉

color = "yellow"

def init(self):

print("我是香蕉")

banana = Banana()

banana.harvest()

上面这么写会报错,不能直接调用基类的__init__()

若是想要在派生类中调用基类__init__()方法,需要在派生类中使用super()函数,下面添加黄色背景部分的代码,注意缩进,是在派生类的__init__()方法中调用基类的__init__()方法。

#水果类为基类,然后创建其派生类的练习

class Fruit: #定义水果类

def init(self,color="green"):

Fruit.color = color

def harvest(self): #定义方法

print("水果开始是:"+Fruit.color+"的!") #输出类的属性

class Banana(Fruit): #定义派生类香蕉

def init(self):

print("我是香蕉")

super().init()

banana = Banana()

banana.harvest()

参考输出:

再举例说明具体的应用:

#水果类为基类,然后创建其派生类的练习

class Fruit: #定义水果类

def init(self,color="green"):

Fruit.color = color

def harvest(self,color): #定义方法

print("水果的颜色是:"+self.color+"的!") #输出类的属性

print("水果开始收获了")

print("水果开始的颜色是:" + Fruit.color + "的!") # 输出类的属性

class Banana(Fruit): #定义派生类香蕉

color = "yellow"

def init(self):

print("我是香蕉")

super().init()#调用时并没有输出,只是传color,基类默认的

class Pear(Fruit): #定义派生类梨

#color = "cyan"

def init(self,color):

print("\n我是梨")

super().init(color) # 调用时并没有输出,只是传color,更改基类默认的

def harvest(self,color):#重写方法

print("梨的颜色是:" + color + "的!")

print("梨收获了!")

print("梨开始是:" + Fruit.color + "的!") # 输出类的属性

banana = Banana()

banana.harvest(banana.color)

pear = Pear("cyan") #创建类的实例

pear.harvest("yellow white")#调用基类的方法

输出结果参考:

今天先写学习到这里了,每天进步一点点。明天也要加油啊!

相关推荐
yangzhi_emo38 分钟前
ES6笔记2
开发语言·前端·javascript
emplace_back2 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk2 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶2 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl2 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
收破烂的小熊猫~2 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式
蹦蹦跳跳真可爱5892 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij3 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
雷羿 LexChien3 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt