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.

继承是面向对象语言的一种功能,其中新类从现有类派生,并导致形成类层次结构。 派生类通常称为子类 ,现有类称为父类 。 继承提供了代码可重用性,从而提高了人员在平台上编写代码的效率。

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是一种面向对象的语言,因此它支持Inheritance的主要功能。 我们还可以通过两个类AB的示例来解释继承。

Let us define these two classes in ruby using its syntax:

让我们使用其语法在ruby中定义这两个类:

    class A
#class methods
end
class B
#class methods
end

If we want to provide inheritance on class B, then the syntax will be changed as,

如果我们要在类B上提供继承 ,则语法将更改为:

    class A
#class methods
end
class B < A
#class methods
end

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

在上面的语法中,我们使用了“符号来继承一个类。现在,如果创建了类B的对象,那么它也将能够使用类A的数据成员和成员方法。这提供了现在的代码可重用性。我们不必定义已经在类A类B中声明的方法,继承后可能有两个类。

  1. Super Class: Super class is the Parent class whose methods are inherited. It can also be termed as Base class.

    超级类 :超级类是其方法被继承的Parent类。 也可以称为基类。

  2. 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仅支持单级继承,这意味着子类只能具有一个基类或父类。 它不允许多级继承,这意味着如果我们要使单个子类具有多个父类,则不可能。 多重继承受到限制,因为它会产生歧义错误,或者如果两个父类中的方法名称相同并且编译器无法决定或选择正确的路径,则可以说它会创建多个路径。

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平台中定义的每个类都有一个默认的父类。 在Ruby 1.9之前,每个类的父类默认情况下都称为“对象类”,而在Ruby 1.9以后,每个类的父类或超类的默认情况下都为“基本对象类”。

Let us understand the practical implementation of Inheritance with the help of the following example,

让我们借助以下示例了解继承的实际实现:

=begin 
Ruby program to demonstrate Inheritance.
=end
class ClassA
def Show
puts "Welcome to IncludeHelp"
end
def Message
puts "Enter your name: "
nm=gets.chomp
puts "Hello #{nm}, I hope you are doing great"
end
end
class ClassB<ClassA
def Hello
puts "Hello World!"
end
end
ob1=ClassB.new
ob1.Show
ob1.Message
ob1.Hello

Output

输出量

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继承

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543719.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题

近期在知乎看到一句话&#xff0c;保持学习的有一种是你看到了很多其它的牛人&#xff0c;不甘心&#xff0c;真的不甘心。Spring和hibernate整合的时候&#xff0c;jsp页面做展现&#xff0c;发现展现属性出现&#xff1a; org.apache.jasper.JasperException: could not init…

sql判断数据库类型数据_SQL数据类型

sql判断数据库类型数据SQL | 资料类型 (SQL | Data Types) Just like other programming languages, facilities of defining data of various types are available in SQL also. SQL supports the following data types for the specification of various data-items or field…

同事反馈环:如何实现持续改进的文化

“魔镜魔镜告诉我&#xff0c;谁才是最美丽的人&#xff1f;”&#xff0c;邪恶的皇后如此问道。似乎在精益和敏捷企业中也会有很多与《白雪公主》中类似的问题&#xff0c;如果我们没有一面可以看到我们正在做什么的镜子&#xff0c;我们就很难搞清楚我们有多么美丽&#xff0…

Scala懒惰瓦尔

Scala | 懒惰的瓦尔 (Scala | lazy val) Scala programming language allows the user to initialize a variable as a lazy val. A lazy variable is used when we need to save memory overheads while object creation. Using the lazy keyword, you can halt the initializ…

经典功率谱估计及Matlab仿真

原文出自&#xff1a;http://www.cnblogs.com/jacklu/p/5140913.html 功率谱估计在分析平稳各态遍历随机信号频率成分领域被广泛使用&#xff0c;并且已被成功应用到雷达信号处理、故障诊断等实际工程中。本文给出了经典功率谱估计的几类方法&#xff0c;并通过Matlab的实验仿真…

ruby 三目运算符_Ruby运算符

ruby 三目运算符Ruby运算符 (Ruby operators) Operators are the symbols which assist compiler or interpreter to carry out certain mathematical, logical and relational tasks and produce the results. Operators are method calls with parameters. 运算符是帮助编译器…

极验验证码流程-3.图片加密处理 图片移位

终于把图片加密给搞定了&#xff0c;原理是他把图分成了52个部分&#xff0c;然后通过移动来形成新的图片 主要的位置关系看代码 顺便吐槽下ruby,小众语言就是这么不方便&#xff0c;很多库都没有&#xff0c;百度了半天 最后换成了java来写 图片保存到本地的就不详细说了 主要…

什么是Brouter?

代理&#xff1a;网络设备 (Brouter: A network device) Brouter is a network device, which operates as a combination of both bridge and router. In this single device, a user will get a function of both bridge and router, as it can send out data to create a co…

11gR2 RAC时间同异常导致节点down掉问题处理

实验环境下11204的RAC环境&#xff0c;出现了一个节点DOWN掉的问题。检查日志信息后&#xff0c;在otcssd日志信息发现如下信息&#xff1a;2016-01-17 23:15:20.564: [ CTSS][1175029504]ctsscomm_recv_cb2: Receive incoming message event. Msgtype [3].2016-01-17 23:15…

html-iframe_HTML iframe

html-iframeiframe (Iframes) In HTML, iframes are used to display a webpage inside another webpage. 在HTML中&#xff0c; iframe用于在另一个网页内显示一个网页。 Syntax: 句法&#xff1a; <iframe src"URL"></iframe>The <iframe> tag…

ruby循环_Ruby循环

ruby循环Ruby循环 (Ruby Loops) Loops are comprised of sequentially group instructions which are executed repeatedly until a certain condition is met. Loops provide great help in making the programmers task easier. 循环由顺序执行的组指令组成&#xff0c;这些指…

后缀数组 TYVJ P1860 后缀数组

/*P1860 后缀数组时间: 1000ms / 空间: 131072KiB / Java类名: Main描述 我们定义一个字符串的后缀suffix(i)表示从s[i]到s[length(s)]这段子串。后缀数组&#xff08;Suffix array&#xff09;SA[i]中存放着一个排列&#xff0c;满足suffix(sa[i])<suffix(sa[i1]) 按照字典…

Binary XML file line #2: You must supply a layout_height attribute inflate

Android开发中遇到的奇葩问题之一&#xff1a;java.lang.NullPointerException&#xff0c;java.lang.RuntimeException:Binary XML file line #2: You must supply a layout_height attribute inflate&#xff0c;遇到这个问题说明你在非主流上测试&#xff0c;或者说是在部分…

量词逻辑量词里面的v表示?_知识表示能力问答中的人工智能量词(MCQ)

量词逻辑量词里面的v表示&#xff1f;1) How many types of quantifiers are there that are used to represent knowledge? 3 types2 typesUser can define as many quantifiers he wantsNone of the above Answer & Explanation Correct answer: 22 types There are two…

给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2...

Prerequisite: 先决条件&#xff1a; Hashing data structure 散列数据结构 Given an array A[] and number X, check for pair in A[] with sum X | using hashing O(n) time complexity | Set 1 给定数组A []和数字X&#xff0c;请检查A []中是否有对X | 使用哈希O(n)时间复…

集合操作(三)Set

2019独角兽企业重金招聘Python工程师标准>>> Set集合 HashSet 哈希表保证元素的唯一性依赖于两个方法一个是hashCode方法一个是equals方法 如果两个对象的hashCode值相同,并且调用该对象的equals方法返回的是true的时候,那么就说明两个对象是相同的 结论&#xff1a…

mcq 队列_MCQ | 软件程序分析工具和组件分类| 免费和开源软件

mcq 队列Q1. Which of the following analysis methods come under Static Analysis Tools? Q1。 静态分析工具包含以下哪些分析方法&#xff1f; Code Walkthrough 代码演练 Code Inspection 代码检查 None of the Above 以上都不是 Both a. & b. 两者都 &#xff06;b。…

samba部署小结

[rootOracle ~]# yum install samba-swat -y[rootOracle ~]# yum install samba-client 客户端工具主配置文件&#xff1a;[rootOracle ~]# cat /etc/samba/smb.conf |grep -v "#"|grep -v "^$"|grep -v ";"[global]workgroup …

JAVA调用动态链接库

上一篇《JAVA本地接口&#xff08;JNI&#xff09;》中介绍了JAVA的JNI技术&#xff0c;通过JAVA自有的方式调用动态链接库&#xff0c;这一篇将继续为大家介绍使用其他方式调用动态链接库。 首先&#xff0c;我们编写一个用于测试的链接库 头文件 print.h #ifdef DLL_IMPLEME…

数组重复次数最多的元素递归_在不使用递归的情况下计算链接列表中元素的出现次数...

数组重复次数最多的元素递归Solution: 解&#xff1a; Input: 输入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head and key is the data of which we have to count the number of occurrences. 一个单链表 &#xff…