pytho基础编程:pycharm实现在子类中添加一个父类没有的属性

Erin ·
更新时间:2024-09-20
· 948 次阅读

这篇文章主要介绍了pycharm实现在子类中添加一个父类没有的属性,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
我就废话不多说了,还是直接看代码吧!

class Car(): """一次模拟汽车的简单尝试""" def __init__(self, make, model, year): """初始化描述汽车的属性""" self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_description_name(self): """返回整洁的描述性信息""" long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): """打印一条指出汽车里程的消息""" print("This car has " + str(self.odometer_reading) + " miles on it.") def update_odometer(self, mileage): """ 将里程读数设置为指定的值 禁止将里程表读数往回调 """ if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self, miles): """将里程表读数增加指定的量""" self.odometer_reading += miles class ElectricCar(Car): """电动汽车的独特之处""" def _init_(self, make, model, year): """ 电动汽车的独特之处 初始化父类的属性,再初始化电动汽车特有的属性 """ super().__init__(make, model, year) self.battery_size = 70 def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kwh battery.") my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla.get_description_name()) my_tesla.describe_battery()

运行结果:

Traceback (most recent call last): File "E:/Python编程从入门到精通配套资料/Self-taught Python/electric_car.py", line 50, in my_tesla.describe_battery() File "E:/Python编程从入门到精通配套资料/Self-taught Python/electric_car.py", line 46, in describe_battery print("This car has a " + str(self.battery_size) + "-kwh battery.") AttributeError: 'ElectricCar' object has no attribute 'battery_size'

补充知识:python中类的继承,子类的方法的添加,子类的方法的覆盖,子类的属性的添加,及继续父类的属性

python如果我们想要继承一个类的方法,并且不改当前类,我们可以新建一个子类,来继续他的方法

1、类的继承,我们先来创建一个Animal的父类,我们再来创建一个dog的子类,dog子类继承父类Animal的方法,但是里面没有执行任何代码,这时我们初始化一下dog子类为dog_1,记得也要传入身高还有体重,不然会报错我们可以发现dog_1继承了Animal里面的属性及方法。直接输出一下及调用一下。

class Animal(): def __init__(self, weight, high): self.weight = weight self.high = high def shout(self): print('wow') class Dog(Animal): pass dog_1 = Dog(20,40) print(dog_1.high) print(dog_1.weight) dog_1.shout() #输出内容是 40 20 wow

2、子类的方法的添加,我们同样可以在dog这个子类里添加新的方法,比如我们添加一个run的方法,再初始化一下dog_1,你会发现dog_1也会有run这个方法。

class Dog(Animal): def run(self): print('running') dog_1 = Dog(20,40) dog_1.run() #输出结果是 running

3、子类的方法的覆盖,我们再添加一个子类Cat,同样继承Animal,但是你发现Cat不是wow这样叫的,所以我们要重新定义一下shout函数,把父类Animal里的函数覆盖掉。这时我们初始化cat_1调用Cat子类,记得要传入体重和身高,不然会报错,然后调用shout方法,你会发现输出的内容是miao,因为子类里的函数把父类里的函数覆盖掉了。

class Cat(Animal): def shout(self): print('miao') cat_1 = Cat(20, 40) cat_1.shout() #输出结果是 miao

4、子类的属性的添加,及继续父类的属性,如果这时我们要给Cat加一个属性color,同样用__init__来定义属性,但是我们要用super()来继承父类里的weight和high,color 属性的性,同样用self.color赋值,这样我们用cat_2初始化Cat类的时候就要传入三个参数,我们输出一个cat_2.color,可以看到是正常调用的。

class Cat(Animal): def __init__(self, weight, high, color): super().__init__(weight, high) self.color = color def shout(self): print('miao') cat_2 = Cat(20, 40, 'yellow') print(cat_2.color) #输出结果是 yellow

类的继承就讲到这里,
内容就以上怎么多,最后给大家推荐一个口碑不错的公众号【程序员学府】,这里有很多的老前辈学习技巧,学习心得,面试技巧,职场经历等分享,更为大家精心准备了零基础入门资料,实战项目资料,每天都有程序员定时讲解Python技术,分享一些学习的方法和需要留意的小细节在这里插入图片描述


作者:程序员学府



父类 属性 子类 pycharm

需要 登录 后方可回复, 如果你还没有账号请 注册新账号