What is “Inheritance”
Inheritance is a concept in object-oriented programming where a class (known as a subclass or derived class) can inherit properties and methods from another class (known as a superclass or base class). The subclass can use the properties and methods of the superclass, and can also add new properties and methods as needed.
Inheritance allows for code reuse and extensibility, as the subclass can reuse the code from the superclass without having to rewrite it. Additionally, the subclass can override methods from the superclass, meaning it can provide a new implementation of a method with different behavior.
In an inheritance relationship, public properties and methods of the superclass are inherited by the subclass, but private properties and methods are not. The subclass can also call methods from the superclass using the super keyword.
Example
|
|
In this example, Animal is a parent class that has an initialize method and a say_hello method. Dog is a subclass that inherits from Animal using < Animal, so it inherits all of Animal’s properties and methods. A new method bark is added in Dog, and Dog can also use Animal’s method say_hello. Through this example, we can see how inheritance works in Ruby.
super
In Ruby, “super” is a keyword used to call a method with the same name as the current method in the parent class. Using “super” in a subclass allows the subclass to inherit relevant behavior from the parent class. When a subclass needs to extend certain methods in the parent class, it can define a method with the same name in the subclass and use the “super” keyword to call the method in the parent class, thus inheriting and extending the parent class’s behavior without duplicating code.
Example with key word super
|
|
In this example, we have the Vehicle class which include an initialize method that takes two parameters make and model. We then create two subclasses, Car and Motorcycle, which inherit from Vehicle and also define their own initialize methods.
In the Car and Motorcycle classes, we use the super keyword to call the initialize method of the parent Vehicle class and pass in the required parameters make and model. We then define our own instance variables @color and @style, respectively.
We create instances of each subclass and call their respective methods. This time, when we create instances of Car and Motorcycle, we need to pass in the additional parameter required by their initialize methods. When we call the start_engine method on these objects, it is inherited from the parent class Vehicle. This demonstrates the use of both inheritance and the super keyword in Ruby.
繼承是什麼
繼承(Inheritance) 是物件導向程式設計中的一個概念,指的是一個類別(稱為子類別或衍生類別)可以繼承另一個類別(稱為父類別或基礎類別)的屬性和方法。子類別可以使用父類別中的屬性和方法,也可以根據需要添加新的屬性和方法。
通過繼承,子類別可以重用父類別的程式碼,並且可以在不影響父類別的情況下進行擴展。這樣可以提高程式碼的可維護性和可重用性。
在繼承關係中,子類別可以繼承父類別的公有屬性和方法,而私有屬性和方法則無法被子類別繼承。子類別可以覆蓋(override)父類別中的方法,即在子類別中重新實現該方法,使其具有不同的行為。此外,子類別還可以調用父類別中的方法,這被稱為 super 調用。
範例說明
|
|
在這個例子中,Animal 是一個父類別,它有一個初始化方法 initialize 和一個 say_hello 方法。Dog 是一個子類別,它通過 < Animal 表示繼承 Animal,因此它繼承了 Animal 的所有屬性和方法。在 Dog 中添加了一個新的方法 bark,而且 Dog 也可以使用 Animal 的方法 say_hello。透過這個範例,我們可以看到繼承如何在 Ruby 中實現。
super
在 Ruby 中,super 是一個關鍵字,用於調用父類別中與當前方法同名的方法。在子類別中使用 super 可以讓子類別繼承父類別中的相關行為。當子類別需要擴展父類別中的某些方法時,可以在子類別中定義一個與父類別中同名的方法,然後使用 super 關鍵字來調用父類別中的方法,以便在不重複代碼的情況下繼承和擴展父類別的行為。
範例(super)
|
|
在這個例子中,新增了 Vehicle 類別,讓它包含一個 initialize 方法,接受兩個參數 make 和 model。然後我們創建了兩個子類別,Car 和 Motorcycle,它們繼承自 Vehicle 並定義了它們自己的 initialize 方法。
在 Car 和 Motorcycle 類別中,我們使用 super 關鍵字來呼叫父類別 Vehicle 的 initialize 方法,並傳遞所需的參數 make 和 model。然後,我們定義了自己的實例變數 @color 和 @style。
我們創建了這兩個子類別的實例,並呼叫它們各自的方法。這次,當我們創建 Car 和 Motorcycle 的實例時,需要傳遞其 initialize 方法所需的額外參數。當我們在這些對象上調用 start_engine 方法時,它是從父類別 Vehicle 繼承而來。這展示了在 Ruby 中使用繼承和 super 關鍵字的方式。