首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pycharm实现在子类中添加一个父类没有的属性

pycharm实现在子类中添加一个父类没有的属性

作者头像
砸漏
发布2020-11-05 11:33:16
6960
发布2020-11-05 11:33:16
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

我就废话不多说了,还是直接看代码吧!

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 <module 
  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

类的继承就讲到这里,大家有问题欢迎私聊我

以上这篇pycharm实现在子类中添加一个父类没有的属性就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档