鱼C论坛

 找回密码
 立即注册
查看: 1043|回复: 4

[已解决]萌新求助,望大佬帮忙

[复制链接]
发表于 2020-8-3 12:21:59 | 显示全部楼层 |阅读模式

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

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

x
class Car():
        def __init__(self,brand,size):
                self.brand=brand
                self.size=size
                self.mile=0
               
        def name(self):
                name=self.brand.title()+' '+self.size.title()
                print(name)
               
        def miles(self):
                print("This car has "+str(self.mile)+' on it.')
               
        def changing_miles(self,miles):
                miles=int(miles)
                if miles>=0:
                        self.mile+=miles
                        print("Now,This car has "+str(self.mile)+' on it.')
                else:
                        print("You can't roll back an odometer!")
                       
#这是之前的一个类


from car import Car

class ElectricCar():
        def __init__ (self,brand,size):
                super(). __init__ (brand,size)
                self.battery=100
                self.rm=100
               
        def battery(self):
                print('The electric capacity of electric vehicles is: '+self.battery)
               
        def rm(self):
                print('The estimated remaining mileage of electric vehicle is: '+self.rm)
               
        def running(self,C):
                C=int(C)
                self.miles+=C
                self.rm-=C
                self.battery-=(0.1)*C

my_new_car=ElectricCar('Tesla','Model S')
my_new_car.running(55)
my_new_car.rm()
my_new_car.battery()

#运行之后pyhton就报错
Traceback (most recent call last):
  File "Electriccar.py", line 21, in <module>
    my_new_car=ElectricCar('Tesla','Model S')
  File "Electriccar.py", line 5, in __init__
    super(). __init__ (brand,size)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

求大佬赐教[拜谢]
最佳答案
2020-8-3 12:32:01



第二个代码应该是想继承 Car 类吧,而且你的 running 方法中的 mile 参数不小心多加了个 s ,应该去掉

你的变量名和类中的函数名相同,导致函数被变量覆盖,你就不能直接调用 rm() 和 battery() 了

而且字符串和整型不能直接进行 + 法操作,应该将两个方法的 print 改成:+ str(self.battery) 和 + (self.rm)


参考代码:
  1. from car import Car


  2. class ElectricCar(Car):
  3.     def __init__(self, brand, size):
  4.         super().__init__(brand, size)
  5.         self.battery = 100
  6.         self.rm = 100

  7.     def battery_(self):
  8.         print('The electric capacity of electric vehicles is: ' + str(self.battery))

  9.     def rm_(self):
  10.         print('The estimated remaining mileage of electric vehicle is: ' + str(self.rm))

  11.     def running(self, C):
  12.         C = int(C)
  13.         self.mile += C
  14.         self.rm -= C
  15.         self.battery -= (0.1) * C


  16. my_new_car = ElectricCar('Tesla', 'Model S')
  17. my_new_car.running(55)
  18. my_new_car.rm_()
  19. my_new_car.battery_()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-3 12:25:54 | 显示全部楼层
要继承自 Car 类

  1. from car import Car


  2. class ElectricCar(Car):
  3.     def __init__(self, brand, size):
  4.         super().__init__(brand, size)
  5.         self.battery = 100
  6.         self.rm = 100

  7.     def battery(self):
  8.         print('The electric capacity of electric vehicles is: ' + self.battery)

  9.     def rm(self):
  10.         print('The estimated remaining mileage of electric vehicle is: ' + self.rm)

  11.     def running(self, C):
  12.         C = int(C)
  13.         self.miles += C
  14.         self.rm -= C
  15.         self.battery -= (0.1) * C


  16. my_new_car = ElectricCar('Tesla', 'Model S')
  17. my_new_car.running(55)
  18. my_new_car.rm()
  19. my_new_car.battery()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-8-3 12:26:09 | 显示全部楼层

你这里 ElectricCar() 类中的   super(). __init__ (brand,size) 是错误的,因为你 ElectricCar() 没有设置继承,那么默认继承 object 类

但是 object 类的 __init__ 方法没有设置传入参数,而你传入了 brand,size 两个参数导致报错

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-3 12:32:01 | 显示全部楼层    本楼为最佳答案   



第二个代码应该是想继承 Car 类吧,而且你的 running 方法中的 mile 参数不小心多加了个 s ,应该去掉

你的变量名和类中的函数名相同,导致函数被变量覆盖,你就不能直接调用 rm() 和 battery() 了

而且字符串和整型不能直接进行 + 法操作,应该将两个方法的 print 改成:+ str(self.battery) 和 + (self.rm)


参考代码:
  1. from car import Car


  2. class ElectricCar(Car):
  3.     def __init__(self, brand, size):
  4.         super().__init__(brand, size)
  5.         self.battery = 100
  6.         self.rm = 100

  7.     def battery_(self):
  8.         print('The electric capacity of electric vehicles is: ' + str(self.battery))

  9.     def rm_(self):
  10.         print('The estimated remaining mileage of electric vehicle is: ' + str(self.rm))

  11.     def running(self, C):
  12.         C = int(C)
  13.         self.mile += C
  14.         self.rm -= C
  15.         self.battery -= (0.1) * C


  16. my_new_car = ElectricCar('Tesla', 'Model S')
  17. my_new_car.running(55)
  18. my_new_car.rm_()
  19. my_new_car.battery_()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-3 17:46:44 | 显示全部楼层
谢谢大佬指教,现在代码可以运行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 12:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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