【问题标题】:Assistance in converting UML Diagram into Ruby Code协助将 UML 图转换为 Ruby 代码
【发布时间】:2017-02-24 20:00:56
【问题描述】:

UML Diagram

这是我的图表的链接。

它们是 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 来检查它是否正常工作。但我被困在实施部分。应该是这样的吗?
  • 您能帮我实现派生类并解释一下您是如何得出这个解决方案的吗?
  • 好的,我明白了。我正在写一个包含很多细节的更长的答案,请坐好。

标签: ruby uml diagram


【解决方案1】:

我们以每月约会为例。 (我会把另一个留给你,所以我不会做你所有的功课:))

这里最顶层的类是Appointment,里面有常用的字段和方法 到另外两个班级。所以我们可以从那里开始。

让我们定义类

class Appointment
end

它需要有一个带有几个字段的初始化器

class Appointment
  def initialize(the_location, the_purpose, the_hour, the_min)
  end
end

现在,由于我们有一个定义了初始化器的类,ruby 将免费为我们提供新方法。

这个类有几个字段,位置,目的,小时,分钟。我们可以将初始化程序中的那些保存到 实例变量。

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

那里。我们现在拥有所有字段、新方法和初始化方法。接下来我们需要位置()。 这是一个 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 是一项非常常见的任务(我们必须在 这个类),红宝石有一个更短的方法来做到这一点。如果我们说 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
end

到目前为止一切顺利。它有一个初始化程序。让我们定义一下

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

我们需要一个 getter 用于 day 字段

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

最后,我们需要定义occurrence_on?。发生_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 吗? 如果你发在这里,我会指点。

【讨论】:

  • 是的,我一定会的。我会读几遍,然后我自己试一试,写完后把它寄给她。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2014-04-21
  • 1970-01-01
  • 2011-06-12
  • 2012-04-03
  • 1970-01-01
  • 2020-01-18
相关资源
最近更新 更多