我正在学习如何在Ruby中使用继承和超级代码。这是我正在读的一本书中的一个练习的延续。
我把我的汽车班设为超级班。然后,MyCar和MyTruck使用超级方法继承Vehicle类的方法。
两个子类都有一个MAX_SPEED常量,每个子类都不同。每个子类都有自己的.speed_up方法,它根据车辆运行的速度输出不同的字符串。
我的代码格式化正确吗?它可以工作,但是我想知道如何改进它,以及在这种情况下我是否也在使用最佳实践。
年份、颜色、模型和汽油参数应该在MyCar和MyTruck子类中吗?还是应该保留在Vehicle类中?
下面的代码也在我的GitHub库中。
require 'date'
class Vehicle
attr_accessor :speed, :on, :color
attr_reader :year, :model, :gallons
def initialize(year, color, model, gallons)
@year = year
@model = model
@gallons = gallons
@color = color
@speed = 0
@on = false
end
@@miles_per_gallon = 0
def self.mileage(tank, miles)
@@miles_per_gallon = miles / tank
end
def brake
if self.speed == 0
puts "You aren't moving! Yet you keep pushing the brake..."
elsif self.speed >= 10
self.speed -= 10
puts "You slow down to #{self.speed} kilometers an hour."
end
end
def shut_off
if self.on == false
puts "Your car is already off."
else
self.on = false
puts "You turn off your car. The engine falls silent."
end
end
def turn_on
if self.on == false
self.on = true
puts "The car's engine rumbles and sparks to life."
else
puts "Your car is already running!"
end
end
def drive(distance)
puts "You drive #{distance} miles."
Vehicle.mileage(self.gallons, distance.to_i)
puts "When you stop in the gas station, you find that your car gets #{@@miles_per_gallon} miles per gallon."
end
def look_at
if Time.new.hour >= 17 && Time.new.hour < 19
puts "The red light of the setting sun sparkles off the paint of your #{model}."
puts "You feel nostalgic."
elsif Time.new.hour >= 19 && Time.new.hour < 6
puts "Your #{model} carves a dark outline in the moon's pale light."
puts "You feel dangerous. Perhaps you should go find a street race?"
else
puts "You gaze at the body of your #{self.year} #{self.model}. Its #{color} paint sparkles in the sunlight."
puts "You feel happy."
end
end
def repaint(color)
self.color = color
puts "You changed your car's color to #{self.color}!"
puts "Mmm, sexy."
end
end
class MyCar < Vehicle
def initialize(year, color, model, gallons)
super(year, color, model, gallons)
end
MAX_SPEED = 180
def speed_up
if self.speed != MAX_SPEED
self.speed += 10
puts "You speed up to #{self.speed} kph."
if self.speed == 120
puts "The engine purrs, the wind flows past the open window next to your seat."
puts "You reach serenity..."
end
else
puts "The engine roars, putting forth maximum effort."
puts "The speedometer is pinned to the max!"
end
end
end
class MyTruck < Vehicle
def initialize(year, color, model, gallons)
super(year, color, model, gallons)
end
MAX_SPEED = 140
def speed_up
if self.speed != MAX_SPEED
self.speed += 10
puts "You speed up to #{self.speed} kph."
if self.speed == 120
puts "The engine rumbles, the wind flows past the open window next to your seat."
puts "You feel calm. If you had a dog, he'd probably be sticking his head out of the window."
end
else
puts "The engine roars, putting forth maximum effort."
puts "Your fuel gauge needle slowly trickles downward..."
end
end
end发布于 2017-09-04 16:57:01
MyTruck和MyCar不需要他们自己的initialize,他们的行为和MyVehicle中的行为是一样的。
if self.on == false可以替换为if !self.on或谓词。
def on?
on
end
def off?
!on
end见http://ruby-for-beginners.rubymonstas.org/objects/predicates.html。
使用self.class.mileage(self.gallons, distance.to_i)可以覆盖类方法mileage。
self.speed == 0可能被self.speed.zero?取代,因为0、1和类似的对象都是Integer对象并响应zero?,尽管这是自以为是的,也就是说,并不是每个对象都响应zero?。
https://codereview.stackexchange.com/questions/174616
复制相似问题