【发布时间】:2017-02-24 20:00:56
【问题描述】:
这是我的图表的链接。
它们是 3 个类。(约会、每月约会和一次性约会)。我在这一点上迷路了。我重看了几次我的讲座,但我无法超越这一点。这只是一个练习题。任何一步一步的帮助都会非常有帮助。
这是我目前拥有的每个代码。
class Appointment
attr_accessor :location, :purpose, :hour, :min
def initialize the_location, the_purpose, the_hour, the_min
@location = the_location
@purpose = the_purpose
@hour = the_hour
@min = the_min
end
def to_s
end
end
class MonthlyAppointment
attr_accessor :location, :purpose, :day, :hour, :min
def initialize the_location, the_purpose, the_day, the_hour, the_min
@location = the_location
@purpose = the_purpose
@hour = the_hour
@min = the_min
end
def occurs_on?(the_year, the_month, the_day)
return @day == the_day
end
end
class OneTimeAppointment
attr_accessor :year, :month, :day
def initialize the_year, the_month, the_day
@year = the_year
@month = the_month
@day = the_day
end
def occurs_on?(the_year, the_month, the_day)
return @year == the_year &&
@month == the_month &&
@day == the_day
end
end
编辑后的帖子:我想这样实现吗...
class Item
def initialize(item_name, quantity)
@item_name = item_name
@quantity = quantity
end
def quantity=(new_quantity)
@quantity = new_quantity
end
def quantity
@quantity
end
end
【问题讨论】:
-
您对哪一部分特别感兴趣?你似乎走在正确的轨道上。您需要实现实例方法,还是只定义一个具有正确接口的类?
-
是的,我需要实现每个类,而不是设置一个 test.rb 来检查它是否正常工作。但我被困在实施部分。应该是这样的吗?
-
您能帮我实现派生类并解释一下您是如何得出这个解决方案的吗?
-
好的,我明白了。我正在写一个包含很多细节的更长的答案,请坐好。