In Ruby is there a way to overload the initialize constructor?

后端 未结 7 1479
死守一世寂寞
死守一世寂寞 2020-11-29 00:58

In Java you can overload constructors:

public Person(String name) {
  this.name = name;
}
public Person(String firstName, String lastName) {
   this(firstNam         


        
相关标签:
7条回答
  • 2020-11-29 01:46
    class Person
      def initialize(name, lastName = nil)
        name = name + " " + lastName unless lastName.nil?
        @name = name
      end
    end
    
    0 讨论(0)
提交回复
热议问题