幻梦魔王 发表于 2020-8-3 12:21:59

萌新求助,望大佬帮忙

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)

求大佬赐教[拜谢]

zltzlt 发表于 2020-8-3 12:25:54

要继承自 Car 类

from car import Car


class ElectricCar(Car):
    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()

Twilight6 发表于 2020-8-3 12:26:09


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

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

Twilight6 发表于 2020-8-3 12:32:01




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

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

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


参考代码:
from car import Car


class ElectricCar(Car):
    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: ' + str(self.battery))

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

    def running(self, C):
      C = int(C)
      self.mile += 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_()

幻梦魔王 发表于 2020-8-3 17:46:44

谢谢大佬指教,现在代码可以运行了
页: [1]
查看完整版本: 萌新求助,望大佬帮忙