📜  红宝石 |遗产

📅  最后修改于: 2022-05-13 01:55:06.213000             🧑  作者: Mango

红宝石 |遗产

Ruby 是理想的面向对象语言。在面向对象的编程语言中,继承是最重要的特性之一。继承允许程序员将一个类的特征继承到另一个类。 Ruby 只支持单类继承,不支持多类继承,但支持mixinsmixin旨在实现 Ruby 中的多重继承,但它只继承接口部分。

继承提供了“可重用性”的概念,即如果程序员想要创建一个新类,并且有一个类已经包含了程序员想要的一些代码,那么他或她可以从现有类派生一个新类。通过这样做,它增加了对现有类的字段和方法的重用,而无需创建额外的代码。

在上图中,A 类是超类,B 类是子类,或者您可以说 B 类是从 A 类(基类派生的。

继承中的关键术语:

  • 超类:其特征被继承的类称为超类基类父类
  • 子类:从另一个类派生的类称为子类派生类子类。除了基类方法和对象之外,还可以添加自己的对象、方法等。

注意:默认情况下,Ruby 中的每个类都有一个父类。在Ruby 1.9之前,Object 类是所有其他类的父类,或者您可以说它是类层次结构的根。但是从Ruby 1.9版本开始, BasicObject 类是 Ruby 中所有其他类的超类(父类)。 Object 类是BasicObject类的子类。

句法:

subclass_name < superclass_name

例子:

Ruby
# Ruby program to demonstrate
# the Inheritance
 
#!/usr/bin/ruby
 
# Super class or parent class
class GeeksforGeeks
 
    # constructor of super class
    def initialize
         
        puts "This is Superclass"
    end
     
    # method of the superclass
    def super_method
         
        puts "Method of superclass"
    end
end
 
# subclass or derived class
class Sudo_Placement < GeeksforGeeks
 
    # constructor of deriver class
    def initialize
 
       puts "This is Subclass"
    end
end
 
# creating object of superclass
GeeksforGeeks.new
 
# creating object of subclass
sub_obj = Sudo_Placement.new
 
# calling the method of super
# class using sub class object
sub_obj.super_method


Ruby
# Ruby program to demonstrate
# Overriding of Parent or
# Superclass method
 
#!/usr/bin/ruby
 
# parent class
class Geeks
     
    # method of the superclass 
    def super_method
         
        puts "This is Superclass Method"
  end
     
end
 
# derived class 'Ruby' 
class Ruby < Geeks
     
    # overriding the method of the superclass 
    def super_method
         
        puts "Override by Subclass"
  end
end
    
# creating object of sub class
obj = Ruby.new
 
# calling the method
obj.super_method


Ruby
# Ruby Program to demonstrate the
# use of super method
 
#!/usr/bin/ruby
 
# base class
class Geeks_1
    
    # method of superclass accepting
    # two parameter
    def display a = 0, b = 0
        puts "Parent class, 1st Argument: #{a}, 2nd Argument: #{b}"
    end
end
 
# derived class Geeks_2
class Geeks_2 < Geeks_1
 
    # subclass method having the same name
    # as superclass
    def display a, b
         
        # calling the superclass method
        # by default it will pass
        # both the arguments
        super
         
        # passing only one argument
        super a
         
        # passing both the argument
        super a, b
         
        # calling the superclass method
        # by default it will not pass
        # both the arguments
        super()
         
        puts "Hey! This is subclass method"
    end
end
 
# creating object of derived class
obj = Geeks_2.new
 
# calling the method of subclass
obj.display "Sudo_Placement", "GFG"


输出:

This is Superclass
This is Subclass
Method of superclass

父类或超类方法的覆盖:方法覆盖是 Ruby 的一个非常有效的特性。在方法覆盖中,子类和超类包含相同的方法名称,但执行不同的任务,或者我们可以说一个方法覆盖了另一个方法。如果超类包含一个方法,而子类也包含相同的方法名称,则子类方法将被执行。

例子:

红宝石

# Ruby program to demonstrate
# Overriding of Parent or
# Superclass method
 
#!/usr/bin/ruby
 
# parent class
class Geeks
     
    # method of the superclass 
    def super_method
         
        puts "This is Superclass Method"
  end
     
end
 
# derived class 'Ruby' 
class Ruby < Geeks
     
    # overriding the method of the superclass 
    def super_method
         
        puts "Override by Subclass"
  end
end
    
# creating object of sub class
obj = Ruby.new
 
# calling the method
obj.super_method 

输出:

Override by Subclass

继承中使用super方法:该方法用于在子类中调用父类方法。如果该方法不包含任何参数,它会自动传递其所有参数。超级方法由super关键字定义。每当您想调用同名的父类方法时,您可以简单地编写supersuper()

例子:

红宝石

# Ruby Program to demonstrate the
# use of super method
 
#!/usr/bin/ruby
 
# base class
class Geeks_1
    
    # method of superclass accepting
    # two parameter
    def display a = 0, b = 0
        puts "Parent class, 1st Argument: #{a}, 2nd Argument: #{b}"
    end
end
 
# derived class Geeks_2
class Geeks_2 < Geeks_1
 
    # subclass method having the same name
    # as superclass
    def display a, b
         
        # calling the superclass method
        # by default it will pass
        # both the arguments
        super
         
        # passing only one argument
        super a
         
        # passing both the argument
        super a, b
         
        # calling the superclass method
        # by default it will not pass
        # both the arguments
        super()
         
        puts "Hey! This is subclass method"
    end
end
 
# creating object of derived class
obj = Geeks_2.new
 
# calling the method of subclass
obj.display "Sudo_Placement", "GFG"

输出:

Parent class, 1st Argument: Sudo_Placement, 2nd Argument: GFG
Parent class, 1st Argument: Sudo_Placement, 2nd Argument: 0
Parent class, 1st Argument: Sudo_Placement, 2nd Argument: GFG
Parent class, 1st Argument: 0, 2nd Argument: 0
Hey! This is subclass method