简体   繁体   English

Ruby类(超级)

[英]Ruby Classes (Super)

I just started classes this week at Bloc. 我这周刚开始在Bloc上课。 I've been practicing with Ruby for about 3 months but I'm finally stuck. 我已经在Ruby上练习了大约3个月,但最终还是陷入了困境。 Hoping to maybe get some help on this. 希望可能对此有所帮助。

The section I'm stuck on pertains to Super 我停留的部分与Super有关

Here are the plain formatted specifications: 以下是普通格式的规范:

  • The President class should accept name and age on initialization, and define getters and setters for those two attributes. President类应在初始化时接受nameage ,并为这两个属性定义getter和setter。

  • FrancePresident and UnitedStatesPresident should inherit from President . FrancePresidentUnitedStatesPresident应该继承President

  • The two child classes of President should define a citizenship class method, which returns "La France" or "The United States of America" , as appropriate. President的两个子类应定义一个citizenship类方法,并根据情况返回"La France""The United States of America"

  • All instances of President should also have citizenship methods, which return the classes' citizenships. President所有实例还应具有citizenship方法,以返回班级的公民身份。

  • All instances of FrancePresident should append a ", bien sur" to their responses when asked for their name , age , or citizenship . 当询问其nameagecitizenship时, FrancePresident所有实例都应在其响应后附加", bien sur" This is a good case for super 这是超级的好例子

Here are the RSpec Specs I'm trying to meet: 这是我要满足的RSpec规格:

describe President do
  describe "initialization" do
    it "takes a name and age, and delegates citizenship to the class default" do
      prez = President.new("George Washington", 283)
      expect( prez.name ).to eq("George Washington")
      expect( prez.age ).to eq(283)
    end
  end

  describe "children" do
    it "is the parent of FrancePresident and UnitedStatesPresident" do
      expect( FrancePresident.superclass ).to eq(President)
      expect( UnitedStatesPresident.superclass ).to eq(President)
    end
  end
end

describe FrancePresident do
  describe "catchphrase" do
    it "sounds just right" do
      expect( FrancePresident.citizenship ).to eq("La France")
      sarcozy = FrancePresident.new("Nicolas Sarkozy", 59)
      expect( sarcozy.citizenship ).to eq("La France, bien sur")
      expect( sarcozy.age ).to eq("59, bien sur")
      expect( sarcozy.name ).to eq("Nicolas Sarkozy, bien sur")
    end
  end
end

And here is my code currently: 这是我目前的代码:

class President
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age = age 
  end
end

class FrancePresident < President
  def self.citizenship
    "La France"
  end
end

class UnitedStatesPresident < President
  def self.citizenship
    "The United States of America"
  end
end

All in all, I'm stuck with trying to get the citizenship class method to work. 总而言之,我坚持尝试使citizenship类方法起作用。 And from the instructions it almost seems like they want me to define a citizenship class method within the President . 从指示看来,他们似乎似乎要我在President内定义citizenship阶级的方法。 But I don't see how that could work and meet the specs because you can't set the citizenship individually for each child as far as I can see. 但是我看不到这怎么运作并符合规范,因为据我所知,您无法为每个孩子单独设置公民身份。

So Basically I'm stuck on the third & fourth objectives. 所以基本上,我坚持第三个和第四个目标。 Please get back to me on this if you've got the time. 如果您有时间,请与我联系。

In your specs you expect two flavors of citizenship responses. 在您的规范中,您希望获得两种citizenship回复。 One with catchphrase, one without. 一种带有流行语,另一种没有。 So you must have two different methods. 因此,您必须有两种不同的方法。 One class method, one instance method. 一种类方法,一种实例方法。 It should go along these lines: 它应该遵循以下原则:

class FrancePresident < President
  def self.citizenship
    "La France"
  end

  # instance method which adds the catchphrase
  def citizenship
    append_catchphrase(self.class.citizenship)
  end

  # here's your super
  def age
    append_catchphrase(super)
  end

  def catchphrase
    'bien sur'
  end

  private

  def append_catchphrase(val)
    [val, catchphrase].join(', ')
  end
end

You're pretty close, you just need to define instance methods in the child classes in addition to their class methods 您已经很接近了,除了它们的类方法外,您还需要在子类中定义实例方法

class FrancePresident < President
  def self.citizenship
    "La France"
  end

  def citizenship
    "La France, bien sur"
  end
end

class UnitedStatesPresident < President
  def self.citizenship
    "The United States of America"
  end

  def citizenship
    "The United States of America"
  end
end

First I think that citizenship method should be instance method, you can define in in your President class and override it in child classes. 首先,我认为公民身份方法应该是实例方法,您可以在President类中定义它,而在子类中覆盖它。 For your last objective here is an example: 最后一个目标是一个示例:

class FrancePresident < President
  # citizenship method
  def name
    super + ", bien sur"
  end 
  def age
   super + ", bien sur"
  end
end

First you are not looking for a class method, it should be instance method. 首先,您不是在寻找类方法,它应该是实例方法。 Here's the refactored code form what I understand: 这是我理解的重构代码形式:

class President
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age = age
  end

  def citizenship
    self.class.citizenship
  end

end

class FrancePresident < President
  END_STRING = ", bien sur"

  def self.citizenship
    "La France"
  end

  def age
    "#{@age}#{END_STRING}"
  end

  def name
    "#{@name}#{END_STRING}"
  end
  def citizenship
    "#{self.class.citizenship}#{END_STRING}"
  end
end

class UnitedStatesPresident < President
  def self.citizenship
    "The United States of America"
  end
end



aa = UnitedStatesPresident.new("Roosevelt", 35)
puts aa.citizenship
puts aa.name
puts aa.age
ff = FrancePresident.new("France", 35)
puts ff.citizenship
puts ff.name
puts ff.age

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM