What is “Polymorphism”
Polymorphism is a concept in object-oriented programming that refers to the ability of an object to take on many forms or have multiple behaviors. Specifically, it allows different objects to be treated as if they were the same type of object, even if they are different. This means that the same method or operation can be used on different objects, and each object will behave differently based on its own internal implementation.
Polymorphism is typically achieved through inheritance and method overriding, which allows a subclass to override the implementation of a method inherited from its superclass. This means that different subclasses of the same superclass can have different behaviors for the same method, and can therefore be treated as if they were the same type of object.
Polymorphism is a key concept in object-oriented programming, as it allows code to be written in a more general and reusable way. By treating objects as if they were the same type of object, code can be written that is more modular, flexible, and easier to maintain, since it can be used with different types of objects without modification.
Example with Ruby
|
|
In this example, we define an Animal class and two subclasses Dog and Cat, both of which override the speak method. We also define an array animals that contains instances of the Animal, Dog, and Cat classes.
In the loop, we pass each object in the animals array as an argument to the speak method. Since each object is an instance of a different class, they will exhibit different behaviors. When the Animal object calls the speak method, it will output “I am an animal”; when the Dog object calls the speak method, it will output “Woof!”; when the Cat object calls the speak method, it will output “Meow!”.
This example demonstrates how polymorphism can be used to implement different behaviors for different objects. Since the Dog and Cat classes both inherit from the Animal class, they both have a speak method, but the implementation of this method is different. When we call the speak method, Ruby automatically chooses the appropriate method based on the actual type of the object. This is the essence of polymorphism.
Example with JavaScript
|
|
多型是什麼?
多型(polymorphism)是面向物件程式設計(OOP)的一個概念,它允許不同的物件具有相同的介面(方法或屬性),但表現出不同的行為。換句話說,多型是指在不同的情境中,同一個實體(例如一個類別的實例)可以表現出不同的形態和行為。
多型的實現方式有很多種,其中最常見的是方法重載(method overloading)和方法重寫(method overriding)。方法重載是指在一個類別中定義多個方法,這些方法具有相同的名稱,但是參數類型或數量不同。當調用這些方法時,編譯器會根據實參的類型和數量自動匹配到對應的方法。方法重寫則是指子類重寫父類的方法,在子類中定義一個和父類方法簽名相同的方法,但是實現不同的行為。
多型是面向物件程式設計的重要特性之一,它可以讓程式更加靈活、可擴展和易維護。通過多型,我們可以編寫通用的程式碼,而不需要針對每個具體的物件編寫不同的程式碼。
在 Ruby 中,多型通常是通過方法重寫(method overriding)來實現的。上面第一個例子是一個簡單的 Ruby 範例,展示了如何使用多型來實現不同物件的不同行為:
在這個例子中,我們定義了一個 Animal 類別和兩個子類別 Dog 和 Cat,它們都重寫了 speak 方法。我們還定義了一個陣列 animals,裡面包含了 Animal、Dog 和 Cat 類別的實例。
在迴圈中,我們將 animals 陣列中的每個物件作為參數傳遞給 speak 方法。由於每個物件都是不同的類別實例,所以它們將表現出不同的行為。當 Animal 物件調用 speak 方法時,它將輸出 “I am an animal”;當 Dog 物件調用 speak 方法時,它將輸出 “Woof!";當 Cat 物件調用 speak 方法時,它將輸出 “Meow!"。
這個例子展示了如何使用多型來實現不同物件的不同行為。由於 Dog 和 Cat 類別都繼承自 Animal 類別,它們都有一個 speak 方法,但是這個方法的實現是不同的。當我們調用 speak 方法時,Ruby 會根據物件的實際類型自動選擇適當的方法。這就是多型的本質。