- 帖子
- 5
- 精华
- 0
- 积分
- 28
- 阅读权限
- 10
- 注册时间
- 2022-9-25
- 最后登录
- 2023-1-3
|
第48课
问题是:
两辆车,速度分别是60和100,要求分别计算从A到B的时间和B到C的时间
标准答案:
class Car:
speed = 0
def drive(self, distance):
time = distance / self.speed
print (time)
car1 = Car()
car1.speed = 60.0
car1.drive(100.0)
car1.drive(200.0)
car2 = Car()
car2.speed = 150.0
car2.drive(100.0)
car2.drive(200.0)
我的答案是:
class vehicle:
dis_A_B = 100
dis_B_C = 200
def time(self, speed):
time_A_B = self.dis_A_B / speed
time_B_C = self.dis_B_C / speed
print('time from A to B is %.2f hrs' % time_A_B)
print('time from B to C is %.2f hrs' % time_B_C)
car = vehicle()
car.time(60)
print('------------')
car_pro = vehicle()
car_pro.time(150)
我的问题是:
我写的这个算不算面向对象?
相比之下哪种更简洁?
|
|