日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ruby 覆盖率测试_Ruby方法覆盖

發(fā)布時(shí)間:2025/3/11 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby 覆盖率测试_Ruby方法覆盖 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

方法覆蓋只是意味著在同一范圍內(nèi)定義了兩種方法,它們都用于執(zhí)行不同的任務(wù)。 此功能以支持Inheritance的面向?qū)ο笳Z言提供。 繼承不過是子類對象可以訪問超類方法的機(jī)制。 在“ 方法重寫”中 ,在基類和派生類中存在兩個(gè)相同名稱的方法,但功能不同。 覆蓋以派生類或子類的方法替換或覆蓋派生類方法的實(shí)現(xiàn)的方式進(jìn)行。

The general view of method overriding looks like,

方法覆蓋的一般視圖如下所示:

class Parentdef Methodendendclass Childdef Methodendend

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:

您可以觀察到兩種方法的名稱相同。 現(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.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. =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.rupee

Output

輸出量

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 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.

上面的代碼有兩個(gè)具有相同名稱“盧比”的方法。 在方法重寫的情況下,子類和超類中都可以存在相同名稱的方法。 在這里,我們正在創(chuàng)建子類和父類的對象,并且它們正在調(diào)用其方法。 如果僅創(chuàng)建了子類對象,則它必須已替換了父類方法。 請記住,您不能覆蓋私有方法。

翻譯自: https://www.includehelp.com/ruby/method-overriding.aspx

ruby 覆蓋率測試

總結(jié)

以上是生活随笔為你收集整理的ruby 覆盖率测试_Ruby方法覆盖的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。