python“类”字的问题

时间:2013-11-05 01:29:47

标签: python class object

问题是

编写一个名为Car的类,它具有以下数据属性:

__year_model(对于汽车的年份模型)

__make(汽车制造的汽车)

__speed(对于汽车当前的速度)

Car类应该有__init__方法接受汽车的年份模型并作为参数。它还应该为__speed数据属性指定0。

该课程还应具备以下方法:

•加速 每次调用时,acceleration方法都应该为速度数据属性添加5。

•刹车 每次调用时,制动方法应从速度数据属性中减去5。

•get_speed get_speed方法应返回当前速度

接下来,设计一个创建汽车对象的程序,然后调用加速方法五次。每次调用加速方法后,获取汽车的当前速度并显示它。然后调用制动方法五次。每次调用制动方法后,获取当前车速并显示它。

我收到错误TypeError: __init__() missing 3 required positional arguments: 'year', 'make', and 'speed'

我的代码:

#create class called car

class Car:
    def __init__(self,year,make,speed):
        self.__year_model=year
        self.__make=make
        self.__speed=0

    #set the arguments for speed,year, and make
    def set_year_model(self,year):
        self.__year_model=year

    def set_make(self,make):
        self.__make=make

    def set_speed(self,speed):
        self.__speed=0

    #the returns for speed, year, and make
    def get_year_model(self):
        return self.__year_model

    def get_make(self):
        return self.__make

    def get_speed(self):
        return self.__speed


    #methods
    def accelerate(self):
        self.speed +=5

    def brake(self):
        self.speed-=5

    def get_speed(self):
        return self.speed

#actuall program
#create car object
def main():
    my_car=Car()

    year=print(input('car year: '))
    make=print(input('car make: '))
    speed= print('Current speed is 0')

    #accelerate 5 times

    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())

    #brake five times
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())

main()

4 个答案:

答案 0 :(得分:2)

__init__类的Car接受三个你没有传递的参数。

year=input('car year: '))
make=input('car make: '))
speed=0
print'Current speed is', speed
#actuall program
#create car object
def main():
my_car=Car(year, make, speed)

print之前,您不需要这些input语句。我不确定你要跟它做什么。 speed= print('Current speed is 0')也不正确。

此函数始终将速度设置回零,如果这是你想要的,那么就不需要传递参数speed。与init相同的是,如果您还没有使用它,则无需传递参数。

def set_speed(self,speed):
    self.__speed=0

答案 1 :(得分:0)

错误消息告诉您究竟出了什么问题。您已将Car定义为需要三个参数:

def __init__(self, year, make, speed):

但是当你创建一个时,你不会传递任何一个:

my_car = Car()

请参阅?括号中没有任何内容。显然这需要:

my_car = Car(year, make, speed)

当然,你不能这样做,因为你还没有要求这些项目。因此,在从用户那里获取该信息之后创建my_car

答案 2 :(得分:0)

class Car(object):#制作称为car的类     def init (自身,模型,制造商):     self .__ model =模型     self .__ make =制造     self .__ speed = 0     def加速(个体经营):     self .__ speed + = 5#增加5速度     def制动器(个体):     self .__ speed-= 5#降低5速度

def get_speed(self):
return self.__speed #return speed

wr_handle = open("output_04.txt", "w+")
carObject = Car( 1984,"Chevron")
wr_handle.write("Car is accelerating: \n")
print("Car is accelerating:") #accelerate 5 times
for p in range(0, 5):
carObject.accelerate()
my_car_speed = carObject.get_speed()
wr_handle.write("The current speed is: %d \n"% my_car_speed)
print("The current speed is: %d "% my_car_speed)

wr_handle.write("Car is braking: \n")
print("Car is braking: ") #brake 5 times
for p in range(0,5):
carObject.brake()
my_car_speed = carObject.get_speed()
wr_handle.write("The current speed is: %d \n"% my_car_speed)
print("The current speed is: %d "% my_car_speed)

wr_handle.write("Bye! \n")
print("Bye! ")
wr_handle.close()

答案 3 :(得分:-1)

class Car(object):#制作称为car的类     def init (自身,模型,制造商):         self .__ model =模型         self .__ make =制造         self .__ speed = 0     def加速(个体经营):         self .__ speed + = 5#提高5速度

def brake(self):
    self.__speed -= 5 #decrease 5 speed