简体   繁体   English

不确定解决方案是否适用于python类

[英]Not sure solution is correct for python classes

Here is the question; 这是问题; create a car class with the following data attributes 使用以下数据属性创建汽车类

__year_model
__make
__speed

The class should have an __init__ method that accepts the car's year model and make as argument. 该类应具有__init__方法,该方法接受汽车的年份模型并将其作为参数。 The value should be assigned to the objects __year_model and __make attributes . 该值应分配给对象__year_model__make attributes It should assign 0 to the __speed data attribute 它应该为__speed数据属性分配0

It should also have the following method: 它还应具有以下方法:

accelerate: accelerate should add 5 to the speed data attribute each time it is called. 加速:每次调用时,加速应在速度数据属性中添加5。

brake: this method should subtract 5 from the speed 刹车:此方法应从速度中减去5

get_speed: this should display the current speed get_speed:这应该显示当前速度

Next, design a program that creates a Car object, and then calls the accelerate method five times. 接下来,设计一个程序,该程序创建一个Car对象,然后调用加速方法五次。 After each call to the accelerate method, get the current speed of the car and display it. 每次调用加速方法后,获取汽车的当前速度并显示出来。 Then call the brake method five times. 然后调用制动方法五次。 After each call to the brake method, get the current speed of the car and display it. 每次调用制动方法后,获取汽车的当前速度并显示出来。

class Car:
    def __init__(self,year_model,make,speed):
        self.year_mode = year_model
        self.make = make
        self.speed = 0

    def accelerate(self,accelerate):
        self.accelerate = accelerate
        speed = 0
        for x in range(5):
            speed = speed + 5
            print speed

    def brake(self,brake):
        self.brake = brake
        speed = 0
        for x in range(5):
            brake = brake - 5
            print brake

    def get_speed(self):
        return self.speed


test = Car(2006,'Honda',100)

I know test is the instance. 我知道测试就是实例。 Is it also the object? 它也是对象吗? Thank you 谢谢

Yes, test is an object, ie an instance of a class is an object. 是的, test是一个对象,即类的实例一个对象。

Your class needs some work. 您的课堂需要做一些工作。 According to the spec, speed is not supposed to be passed to __init__() . 根据规范,不应将speed传递给__init__() Remove that and initialise self.__speed to 0 in __init__() . 删除它并在__init__()中将self.__speed初始化为0

accelerate() and brake() are not supposed to take arguments - each method should either increase the current speed by 5, or reduce the speed by 5. Therefore these methods do not require any arguments (other than self ). accelerate()brake()不应带有参数-每个方法都应将当前速度提高5,或将速度降低5。因此,这些方法不需要任何参数( self除外)。

So accelerate() should be this: 所以accelerate()应该是这样的:

def accelerate(self):
    self.__speed += 5    # shorthand for self.speed = self.speed + 5

which simply increases the speed by 5 each time it is called. 每次调用只会将速度提高5。 Note that self is used so that the member __speed is incremented, not some temporary local varible. 请注意,使用self是为了使成员__speed增加,而不是某些临时的局部变量。 You can do the same for brake() . 您可以对brake()执行相同的操作。

Other than that, add the tests that your assignment specifies: call accelerate 5 times printing the speed after each call. 除此之外,添加分配指定的测试:每次调用后,调用accelerate 5倍打印速度。 Similarly for the brake method. brake方法类似。 Does the output agree with what you would expect? 输出是否符合您的期望?

Test like this: 像这样测试:

car = Car(2006,'Honda')

for i in range(5):
    car.accelerate()
    print(car.get_speed())

for i in range(5):
    car.brake()
    print(car.get_speed())

Oh, and name your members as per the spec: __speed , not speed etc. 哦,按照规范命名您的成员: __speed ,而不是speed等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM