ruby 覆盖率测试_Ruby方法覆盖
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 Parentdef Methodendendclass Childdef MethodendendYou 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 =endclass Parentdef prntfor i in 0..5puts "Parent class method"endend endclass Child < Parentdef prnt for i in 0..5puts "Child class method"endend endob1=Child.new #class instantiation ob1.prntOutput
輸出量
Child class method Child class method Child class method Child class method Child class method Child class methodYou 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. =endclass Dollardef initializeputs "Enter amount in dollar"@dlr=gets.chomp.to_fenddef rupeeputs "#{@dlr}$ = #{71.23*@dlr} inr"end endclass Yen < Dollardef initializeputs "Enter amount in Yen" @yn=gets.chomp.to_fenddef rupeeputs "#{@yn} Yen = #{0.67*@yn} inr"end endob1=Dollar.new ob1.rupee ob2=Yen.new ob2.rupeeOutput
輸出量
Run 1: Enter amount in dollar 12 12.0$ = 854.76 inr Enter amount in Yen 900 900.0Yen = 603.0 inrRun 2: Enter amount in dollar 190.23 190.23$ = 13550.0829 inr Enter amount in Yen 890.23 890.23 Yen = 596.4541 inrThe 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 覆蓋率測試
總結
以上是生活随笔為你收集整理的ruby 覆盖率测试_Ruby方法覆盖的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby 将字符串转为数组_Ruby程序
- 下一篇: vector cbegin_vector