鱼C论坛

 找回密码
 立即注册
查看: 2194|回复: 5

[已解决]对象没有属性

[复制链接]
发表于 2017-11-29 13:13:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class Car():
    def __init__(self,make,model,year):
        self.make = make
        self.model = model
        self.year = year

    def get_descriptive_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.")


class Battery():
    def __init__(self,battery_size = 70):
        self.battery_size = battery_size
    def describe_battery(self):
        print("This car has a " + str(self.battery_size) + "-KWh battery.")

    def get_range(self):
        if self.battery_size == 70:
            range = 240

        elif self.battery_size == 85:
            range = 270

        message = "This car can go approximately " + str(range)
        message += "miles on a full charge."
        print(message)
    def upgrade_battery(self):
        if self.battery_size != 85:
            self.battery_size = 85
        print("This car can go approximately" + " " + str(self.battery-size))   


        
class ElectricCar(Car):
    def __init__(self,make,model,year):
        super().__init__(make,model,year)
   
        self.battery = Battery()
   
   
my_tesla =  ElectricCar('tesla','mosel s',2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.upgrade_battery()        


Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36/car.py", line 50, in <module>
    my_tesla.battery.upgrade_battery()
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36/car.py", line 35, in upgrade_battery
    print("This car can go approximately" + " " + str(self.battery-size))
AttributeError: 'Battery' object has no attribute 'battery'


问题:属性class Battery():
    def __init__(self,battery_size = 70):
        self.battery_size = battery_size   我设置过了啊,怎么对象没有属性呢?
最佳答案
2017-11-29 13:15:40
中划线改成下划线
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-11-29 13:15:40 | 显示全部楼层    本楼为最佳答案   
中划线改成下划线
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-29 13:29:43 | 显示全部楼层
self.battery-size
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-29 13:35:06 | 显示全部楼层
BngThea 发表于 2017-11-29 13:15
中划线改成下划线

我检查了一遍,没有中划线啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-29 13:37:12 From FishC Mobile | 显示全部楼层
pythonwei 发表于 2017-11-29 13:35
我检查了一遍,没有中划线啊

我都看到了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-29 13:43:03 | 显示全部楼层

哦  ,看到了  ,看多了    花眼了都
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-4 09:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表