ruby继承_Ruby继承
ruby繼承
Ruby中的繼承 (Inheritance in Ruby)
Inheritance is a feature of Object Oriented languages in which new classes are derived from existing classes and resulting in the formation of a hierarchy of classes. The derived class is often called as child class and the existing class is termed as parent class. Inheritance provides code reusability which increases the human efficiency to write codes on the platform.
繼承是面向?qū)ο笳Z言的一種功能,其中新類從現(xiàn)有類派生,并導(dǎo)致形成類層次結(jié)構(gòu)。 派生類通常稱為子類 ,現(xiàn)有類稱為父類 。 繼承提供了代碼可重用性,從而提高了人員在平臺上編寫代碼的效率。
Ruby is an Object Oriented language, thus it supports the major feature of Inheritance. We can also explain inheritance via an example of two classes namely A and B.
Ruby是一種面向?qū)ο蟮恼Z言,因此它支持Inheritance的主要功能。 我們還可以通過兩個類A和B的示例來解釋繼承。
Let us define these two classes in ruby using its syntax:
讓我們使用其語法在ruby中定義這兩個類:
class A#class methodsendclass B#class methodsendIf we want to provide inheritance on class B, then the syntax will be changed as,
如果我們要在類B上提供繼承 ,則語法將更改為:
class A#class methodsendclass B < A#class methodsendIn the above syntax that we have used the " symbol to inherit a class. Now, if the object of class B is created then it will also be able to use the data members and member methods of class A. This provides code reusability as now we don't have to define methods which are already declared in class A, in class B as well. There are two classes possible after inheritance.
在上面的語法中,我們使用了“符號來繼承一個類。現(xiàn)在,如果創(chuàng)建了類B的對象,那么它也將能夠使用類A的數(shù)據(jù)成員和成員方法。這提供了現(xiàn)在的代碼可重用性。我們不必定義已經(jīng)在類A和類B中聲明的方法,繼承后可能有兩個類。
Super Class: Super class is the Parent class whose methods are inherited. It can also be termed as Base class.
超級類 :超級類是其方法被繼承的Parent類。 也可以稱為基類。
Sub Class: Sub class is often termed as Derived or Child class. Sub class derives the methods and variables of Base class or Parent class.
子類 :子類通常被稱為派生類或子類。 子類派生基類或父類的方法和變量。
Ruby supports only single level inheritance which means that a child class can have only one base class or parent class. It disallows Multi-level inheritance which means that if we want to make multiple parent classes of a single child class then it is not possible. Multiple inheritances are restricted because it creates ambiguity error or you can say that it creates multiple paths if the method name is same in both parent classes and the compiler could not decide or choose the right path.
Ruby僅支持單級繼承,這意味著子類只能具有一個基類或父類。 它不允許多級繼承,這意味著如果我們要使單個子類具有多個父類,則不可能。 多重繼承受到限制,因為它會產(chǎn)生歧義錯誤,或者如果兩個父類中的方法名稱相同并且編譯器無法決定或選擇正確的路徑,則可以說它會創(chuàng)建多個路徑。
Every class which is defined in Ruby platform has a default parent class. Before Ruby 1.9 version, every class has the parent class known as "Object class" by default but after Ruby 1.9 version, the parent class or the superclass of every class is "Basic Object class" by default.
Ruby平臺中定義的每個類都有一個默認(rèn)的父類。 在Ruby 1.9之前,每個類的父類默認(rèn)情況下都稱為“對象類”,而在Ruby 1.9以后,每個類的父類或超類的默認(rèn)情況下都為“基本對象類”。
Let us understand the practical implementation of Inheritance with the help of the following example,
讓我們借助以下示例了解繼承的實際實現(xiàn):
=begin Ruby program to demonstrate Inheritance. =endclass ClassAdef Showputs "Welcome to IncludeHelp"enddef Messageputs "Enter your name: "nm=gets.chompputs "Hello #{nm}, I hope you are doing great"end endclass ClassB<ClassAdef Helloputs "Hello World!"end endob1=ClassB.new ob1.Show ob1.Message ob1.HelloOutput
輸出量
Welcome to IncludeHelp Enter your name: Hrithik Hello Hrithik, I hope you are doing great Hello World!You can observe in the above code that, ClassB is the child class of Base class ClassA. The object of ClassB can access the methods of ClassA.
您可以在上面的代碼中觀察到, ClassB是基類ClassA的子類。 ClassB的對象可以訪問ClassA的方法。
翻譯自: https://www.includehelp.com/ruby/inheritance.aspx
ruby繼承
總結(jié)
以上是生活随笔為你收集整理的ruby继承_Ruby继承的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql5.5.20安装_mysql5
- 下一篇: java框架异常怎么处理_java异常处