How do I make a class whose constructor looks like the constructor of a built-in class?

前端 未结 2 1413
-上瘾入骨i
-上瘾入骨i 2020-12-21 06:02

Complex is a built-in class. To make a Complex object, I write:

Complex(10, 5)

But if I create my own class

2条回答
  •  借酒劲吻你
    2020-12-21 06:33

    class Dog
      def initialize(name)
        @name = name
      end
    
      def greet
        puts 'hello'
      end
    end
    
    
    def Dog(x)
      Dog.new(x)  #Create a new instance of the Dog class and return it.
    end
    
    
    d = Dog("Rover")
    d.greet
    
    --output:--
    hello
    

提交回复
热议问题