ruby 覆盖率测试_Ruby方法覆盖
ruby 覆蓋率測(cè)試
Ruby中的方法重寫(xiě) (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.
方法覆蓋只是意味著在同一范圍內(nèi)定義了兩種方法,它們都用于執(zhí)行不同的任務(wù)。 此功能以支持Inheritance的面向?qū)ο笳Z(yǔ)言提供。 繼承不過(guò)是子類(lèi)對(duì)象可以訪問(wèn)超類(lèi)方法的機(jī)制。 在“ 方法重寫(xiě)”中 ,在基類(lèi)和派生類(lèi)中存在兩個(gè)相同名稱(chēng)的方法,但功能不同。 覆蓋以派生類(lèi)或子類(lèi)的方法替換或覆蓋派生類(lèi)方法的實(shí)現(xiàn)的方式進(jìn)行。
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:
您可以觀察到兩種方法的名稱(chēng)相同。 現(xiàn)在,借助示例,讓我們了解它們的功能差異:
=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.
您可以在上面的代碼中觀察到子類(lèi)和父類(lèi)方法都具有相同的名稱(chēng),但它們的用途不同。
Go through the example given below to understand the concept in a broader way,
通過(guò)下面給出的示例,可以更廣泛地理解該概念,
=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.
上面的代碼有兩個(gè)具有相同名稱(chēng)“盧比”的方法。 在方法重寫(xiě)的情況下,子類(lèi)和超類(lèi)中都可以存在相同名稱(chēng)的方法。 在這里,我們正在創(chuàng)建子類(lèi)和父類(lèi)的對(duì)象,并且它們正在調(diào)用其方法。 如果僅創(chuàng)建了子類(lèi)對(duì)象,則它必須已替換了父類(lèi)方法。 請(qǐng)記住,您不能覆蓋私有方法。
翻譯自: https://www.includehelp.com/ruby/method-overriding.aspx
ruby 覆蓋率測(cè)試
總結(jié)
以上是生活随笔為你收集整理的ruby 覆盖率测试_Ruby方法覆盖的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ruby 将字符串转为数组_Ruby程序
- 下一篇: vector cbegin_vector