ruby 构造函数
Ruby中的构造函数 (Constructors in Ruby)
Constructors are one of the most important parts of Object-Oriented programming. A constructor of a class is a unique method which is invoked or called when an object of that particular class is being instantiated. It does not require an explicit statement for their calling. The main purpose of constructors is to facilitate initialization to the variables of the class during the process of object creation. Constructors work same as a normal method, they do contain statements, expressions, and method calls.
构造函数是面向对象编程中最重要的部分之一。 类的构造函数是唯一的方法,当实例化该特定类的对象时,将调用该方法。 不需要显式声明即可进行调用。 构造函数的主要目的是在对象创建过程中促进对类变量的初始化。 构造函数的工作方式与普通方法相同,它们确实包含语句,表达式和方法调用。
In other object-oriented languages like C++ and Java, the name of class and constructor is used to be the same but in the case of Ruby, the name is different from the name of the class. In Ruby, you can define a constructor with the help of the following syntax,
在其他面向对象的语言(例如C ++和Java)中,类和构造函数的名称曾经是相同的,但是在Ruby的情况下,该名称与类的名称不同。 在Ruby中,您可以借助以下语法来定义构造函数,
def initialize
#expressions
end
A constructor returns the object of that class. It is not possible to provide an inheritance to the constructor whereas it is permissible to overload a constructor. The constructors are defined within a class in the following way:
构造函数返回该类的对象。 无法向构造函数提供继承,而允许重载构造函数。 构造器是通过以下方式在类中定义的:
class CLASS_NAME
def initialize
#expressions
end
#other methods
end
The above is a syntax of a constructor with no arguments. Go through the following syntax to understand constructor with arguments,
上面是没有参数的构造函数的语法 。 通过以下语法了解带参数的构造函数 ,
带参数的构造函数 (Constructor with arguments)
class CLASS_NAME
def initialize(#argument list)
#expressions
end
#other methods
end
Remember that – a constructor is a special method of a class. It does not require an explicit call. In Ruby, constructors are invoked with the help of "new" keyword and a new keyword is used for creating an object of the class. The "new" keywords give an internal call to the "initialize" method and if the new has got some arguments specified, it will be passed to the initialize method. If the constructor is not parametric and “new” has some arguments then the compiler will generate an error known as "initialize: wrong number of arguments".
请记住 – 构造函数是类的一种特殊方法 。 它不需要显式调用。 在Ruby中, 构造函数是在“ new”关键字的帮助下调用的,而new关键字用于创建该类的对象。 “ new”关键字对“ initialize”方法进行内部调用,如果指定了新参数,则将其传递给initialize方法。 如果构造函数不是参数化的,并且“ new”具有一些参数,则编译器将生成一个错误,称为“ initialize:错误的参数数目” 。
Let us understand the example of a non-parametric constructor
让我们了解一个非参数构造函数的示例
=begin
Ruby program to demonstrate constructor
without parameters
=end
class ClassA
#non parametric constructor
def initialize
for i in 0..10
puts "Welcome to Includehelp.com"
end
end
end
#class instantiation
ClassA.new
Output
输出量
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
You can observe in the above code that when we are giving an explicit call to "new" keyword, it is in return giving an internal call to initialize method.
您可以在上面的代码中观察到,当我们显式调用“ new”关键字时,作为回报,它给出了对initialize方法的内部调用。
Now, understand parametric constructor with the help of example given below,
现在, 借助以下示例了解参数构造函数 ,
=begin
Ruby program to demonstrate
constructor with parameters
=end
class ClassA
#parametric constructor
def initialize(a,b)
@instA=a
@instB=b
end
def prnt
puts "First value is : #{@instA}"
puts "Second value is : #{@instB}"
end
end
#class instantiations
ob1=ClassA.new(10,20)
ob2=ClassA.new(13,200)
ob1.prnt
ob2.prnt
Output
输出量
First value is : 10
Second value is : 20
First value is : 13
Second value is : 200
In the above example, you can observe that we are providing values to "new" and "new" is proving those values to initialize method.
在上面的示例中,您可以观察到我们正在为“ new”提供值,而“ new”正在证明这些值用于初始化方法。
ruby 构造函数