ruby继承
Ruby| 使用继承的多态 (Ruby | Polymorphism using Inheritance)
In this tutorial, we will study about polymorphism in Ruby. We all know that Ruby is a pure object-oriented language and polymorphism is one of the very important features of any object-oriented language. You must be having a little difficulty in understanding the topic with its title, but do not worry, you will be having your all doubts clear after going through the article. The rest of the content is all about polymorphism in Ruby along with syntaxes and demonstrating program codes for your better understanding.
在本教程中,我们将研究Ruby中的多态性 。 众所周知,Ruby是一种纯粹的面向对象语言,而多态性是任何面向对象语言的重要特征之一。 您一定很难理解带有标题的主题,但是不要担心,阅读本文后,您的所有疑惑都将清除。 其余内容全部涉及Ruby中的多态性以及语法和演示程序代码,以使您更好地理解。
We all know this but let me repeat this that Polymorphism is an addition of two words which are "poly" and "Morph". Here the word "poly" means many and "Morph" means forms. So collectively we can say that Polymorphism is a property in which something has many forms. SP, in computer terminology, polymorphism can be called as a method in which a user can invoke the same method passing different objects as parameters.
我们都知道这一点,但让我再说一遍,多态是两个单词“ poly”和“ Morph”的加法。 此处的“ poly”一词是指许多,而“ Morph”是指形式。 因此,总的来说,我们可以说多态是一种事物具有多种形式的属性。 在计算机术语SP中,可以将多态称为一种方法,其中用户可以调用传递不同对象作为参数的相同方法。
Polymorphism is a property in which classes have different types of functions but they share the same interface. Polymorphism can be studied under two types of sub-categories which are namely
多态是一个属性,其中类具有不同类型的功能,但它们共享相同的接口。 可以在两种子类别下研究多态性,即
Polymorphism with the help of Inheritance
在继承的帮助下实现多态
Polymorphism with the help of Duck-Typing
鸭式打字帮助多态
In this tutorial, we will only study a subcategory which is Polymorphism with the help of Inheritance.
在本教程中,我们将仅在继承的帮助下研究多态性子类别。
Let us understand Inheritance first, it is a property in which child class inherits the property of parent class.
首先让我们了解继承 ,这是子类继承父类属性的属性。
You can understand the implementation of polymorphism with the help of inheritance with the help of following program code,
您可以在以下程序代码的帮助下,通过继承来了解多态的实现 ,
Example:
例:
=begin
Ruby program to demonstrate polymorphism
with Inheritance
=end
class Fruit
def tastetype
puts "Sour fruit."
end
end
class Orange<Fruit
def tastetype
puts "Sour-Sweet Fruit"
end
end
class Apple<Fruit
def tastetype
puts "Sweet Fruit"
end
end
fruit = Fruit.new
fruit.tastetype
fruit = Orange.new
fruit.tastetype
fruit = Apple.new
fruit.tastetype
Output
输出量
Sour fruit.
Sour-Sweet Fruit
Sweet Fruit
Explanation:
说明:
With the help of the above code, you must have got a clear understanding of Polymorphism with the help of Inheritance. Here the method tastetype is invoked with the help of objects Orange and Apple. The Orange and Apple classes have the same parent class fruit. tastetype is the method which they have inherited from their parent class Fruit.
在上述代码的帮助下,您必须借助Inheritance清楚地了解了多态性 。 这里, 滋味类型方法是在对象Orange和Apple的帮助下调用的。 Orange和Apple类具有相同的父类水果 。 avouritetype是他们从其父类Fruit继承的方法。
Now, let us understand Duck Typing, it is nothing but an idea to see what can be done by an object instead of what it can do or in the other words you can say that what can be implemented on the object rather than on the class of the instance.
现在,让我们了解Duck Typing ,这只是一个想法,它可以看到某个对象可以做什么,而不是它可以做什么,或者换句话说,您可以说可以在该对象上而不是在类上实现什么实例的
You can understand the implementation of polymorphism with the help of duck typing with the help of following program code,
您可以在以下程序代码的帮助下,通过鸭子输入来了解多态性的实现 ,
Example:
例:
=begin
Ruby program to demonstrate polymorphism
using duck typing
=end
class College
def enters
puts "A student enters"
end
def type(student)
student.type
end
def course(student)
student.course
end
end
class Ug
def type
puts "student is a under graduate student"
end
def course
puts "BCA student"
end
end
class Pg
def type
puts "student is a post graduate student"
end
def course
puts "MCA student"
end
end
college = College.new
puts "This student is Under graduate"
stu = Ug.new
college.type(stu)
college.course(stu)
puts "This student is Post graduate"
stu = Pg.new
college.type(stu)
college.course(stu)
Output
输出量
This student is Under graduate
student is a under graduate student
BCA student
This student is Post graduate
student is a post graduate student
MCA student
Explanation:
说明:
With the help of the above code, you must have got a clear understanding of Polymorphism with the help of Duck Typing. Here the object stu is playing a role in working with the functionalities of the class College such as its type and its course.
在上述代码的帮助下,您必须借助Duck Typing清楚地了解了多态性 。 在这里,对象stu在使用College类的功能(例如其类型和课程)方面发挥着作用。
翻译自: https://www.includehelp.com/ruby/polymorphism-using-inheritance.aspx
ruby继承