让我们每月约会的例子。 (我会离开另一个为你,所以我没有做你所有的功课:))
这里的顶级类是约会,其中有一些字段和方法是共同的 其他两个类。所以我们可以从那里开始。
让我们定义的类
class Appointment
end
它需要有一个初始化,需要一个几个字段现在
class Appointment
def initialize(the_location, the_purpose, the_hour, the_min)
end
end
,因为我们有我们上定义的初始化类,红宝石将为我们提供免费的新方法。
这个类有它的几个字段,位置,目的,小时,分钟。我们可以将这些从初始化程序保存到 实例变量中。
class Appointment
def initialize(the_location, the_purpose, the_hour, the_min)
@location = the_location
@purpose = the_purpose
@hour = the_hour
@min = the_min
end
end
那里。我们现在有所有的字段,新的方法和初始化方法。接下来,我们需要location()。 这是一个getter,一个获取该字段值的方法。
class Appointment
def initialize(the_location, the_purpose, the_hour, the_min)
@location = the_location
@purpose = the_purpose
@hour = the_hour
@min = the_min
end
def location
return @location
end
end
我们走了。但是正如它发生的那样,因为写一个这样的getter是一个很常见的任务(我们必须在这个类中执行5次),ruby的方法要简短得多。如果我们说attr_reader:location,ruby知道定义一个方法 ,就像上面那个一样。因此,我们的新课程是这样的:
class Appointment
attr_reader :location
def initialize(the_location, the_purpose, the_hour, the_min)
@location = the_location
@purpose = the_purpose
@hour = the_hour
@min = the_min
end
end
我们也可以为其他四个领域做同样的事情。
class Appointment
attr_reader :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
end
好的,现在我们除了to_s()之外都有其他的东西。 to_s是一个返回约会字符串版本的方法。 基本上是一个大致可读的描述。让我们来定义
class Appointment
attr_reader :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
"<Appointment #{@hour}:#{@min} at #{@location} for #{@purpose}>"
end
end
这将会给我们描述东西,如“预约2:34在办公室的最新进展”。 这对我来说很好!
现在我们完成了Appointment类,让我们来做MonthlyAppointment类。 显然它应该是Appointment的一个子类,即使你的图没有显示出来。
到目前为止好。它有一个初始化程序。让我们定义
class MonthlyAppointment < Appointment
def initialize(the_location, the_purpose, the_day, the_hour, the_min)
end
end
在那里,这符合UML为初始化接口指定的内容。 现在,我们看到有一个名为“@day”的字段,并且由于初始化程序在一天中有一个参数,因此我们可以从初始值设定项中保存该值。我们仍然想要保存超类 也关心的所有内容,所以我们调用super来让父类的初始化器也运行。
class MonthlyAppointment < Appointment
def initialize(the_location, the_purpose, the_day, the_hour, the_min)
super(the_location, the_purpose, the_hour, the_min)
@day = the_day
end
end
,我们需要一天领域
class MonthlyAppointment < Appointment
attr_reader :day
def initialize(the_location, the_purpose, the_day, the_hour, the_min)
super(the_location, the_purpose, the_hour, the_min)
@day = the_day
end
end
最后,我们需要定义occurs_on一个getter? occurs_on?需要一年的一个月和一天,并将返回真或假。 带问号的方法应始终返回true或false。 现在,每年都会有一个月的预约,所以我们现在可以忽略它。它也发生在每个月,所以我们可以忽略这一点。但它只发生在每个月的特定日期 ,所以让我们比较被问到的那一天和我们保存的那一天。 如果它们匹配,那么我们应该返回true。例如,如果您有15日的每月预约, 您只需知道它是否是第15日,或者不知道它是否在某个日期发生。
class MonthlyAppointment < Appointment
attr_reader :day
def initialize(the_location, the_purpose, the_day, the_hour, the_min)
super(the_location, the_purpose, the_hour, the_min)
@day = the_day
end
def occurs_on?(the_year, the_mon, the_day)
if @day == the_day
return true
else
return false
end
end
end
瞧!那应该是你的MonthlyAppointment类。你可以尝试实施OneTimeAppointment吗? 如果你在这里发布,我会指出。
你失去了什么特别的部分?你似乎在正确的轨道上。你需要实现实例方法,还是只需定义一个具有正确接口的类? –
是的,我需要实现每个类,而不是建立一个test.rb来检查它是否全部有效。但是我坚持实施部分。它是否应该这样去? –
你能帮我实施派生类,并解释你是如何来到这个解决方案的? –