ruby 覆盖率测试
Ruby中的方法重写 (Method overriding in Ruby)
Method overriding simply means that there are two methods defined within the same scope and they both are used for performing different tasks. This feature is provided in an Object-oriented language which supports Inheritance. Inheritance is nothing but a mechanism through which the child class object can access the methods of the superclass. In Method overriding, two same name methods are present in the base class as well as in derived class but with different functionality. Overriding takes place in the manner that the method of derived class or subclass replaces or overrides the implementation of Derived class method.
方法覆盖只是意味着在同一范围内定义了两种方法,它们都用于执行不同的任务。 此功能以支持Inheritance的面向对象语言提供。 继承不过是子类对象可以访问超类方法的机制。 在“ 方法重写”中 ,在基类和派生类中存在两个相同名称的方法,但功能不同。 覆盖以派生类或子类的方法替换或覆盖派生类方法的实现的方式进行。
The general view of method overriding looks like,
方法覆盖的一般视图如下所示:
class Parent
def Method
end
end
class Child
def Method
end
end
You can observe that name of both the methods is the same. Now, let us understand how they differ in functionality with the help of an example:
您可以观察到两种方法的名称相同。 现在,借助示例,让我们了解它们的功能差异:
=begin
Ruby program to demonstrate method overriding
=end
class Parent
def prnt
for i in 0..5
puts "Parent class method"
end
end
end
class Child < Parent
def prnt
for i in 0..5
puts "Child class method"
end
end
end
ob1=Child.new #class instantiation
ob1.prnt
Output
输出量
Child class method
Child class method
Child class method
Child class method
Child class method
Child class method
You can observe in the above code that both the child and parent class method has the same name but are used for different purposes.
您可以在上面的代码中观察到子类和父类方法都具有相同的名称,但它们的用途不同。
Go through the example given below to understand the concept in a broader way,
通过下面给出的示例,可以更广泛地理解该概念,
=begin
Ruby program to demonstrate method overriding.
=end
class Dollar
def initialize
puts "Enter amount in dollar"
@dlr=gets.chomp.to_f
end
def rupee
puts "#{@dlr}$ = #{71.23*@dlr} inr"
end
end
class Yen < Dollar
def initialize
puts "Enter amount in Yen"
@yn=gets.chomp.to_f
end
def rupee
puts "#{@yn} Yen = #{0.67*@yn} inr"
end
end
ob1=Dollar.new
ob1.rupee
ob2=Yen.new
ob2.rupee
Output
输出量
Run 1:
Enter amount in dollar
12
12.0$ = 854.76 inr
Enter amount in Yen
900
900.0Yen = 603.0 inr
Run 2:
Enter amount in dollar
190.23
190.23$ = 13550.0829 inr
Enter amount in Yen
890.23
890.23 Yen = 596.4541 inr
The above code has two methods with the same name 'rupees'. This is the case of method overriding where same name methods can exist in both child class and superclass. Here we are creating objects of child class and parent class and they are invoking their methods. If only child class object has created, then it must have replaced the parent class method. Remember that you can not override the private methods.
上面的代码有两个具有相同名称“卢比”的方法。 在方法重写的情况下,子类和超类中都可以存在相同名称的方法。 在这里,我们正在创建子类和父类的对象,并且它们正在调用其方法。 如果仅创建了子类对象,则它必须已替换了父类方法。 请记住,您不能覆盖私有方法。
翻译自: https://www.includehelp.com/ruby/method-overriding.aspx
ruby 覆盖率测试