Skip to content
ポリモーフィズム

File:UML_class_pet.svg · Wikimedia Commons · See Wikimedia Commons

EntityQ3240252· pop 46· linked from 181 articles

ポリモーフィズム

Sign in to save

Also known as polymorphism (computer science), Polymorfism

in programming languages and type theory, accessing different types using a common interface

Described at

polymorphism | Python Glossary – Real Python

A concept in object-oriented programming (OOP) that allows objects of different classes to be treated the same if they share the same interface.

realpython.com

Polymorphism enables you to use a single interface to represent different underlying classes. In Python, polymorphism is achieved primarily through duck typing , but you can also implement it through inheritance and method overriding . Polymorphism enhances flexibility and maintainability by allowing you to write more generic and reusable code. class Duck: def swim(self): print("The duck is swimming.") class Albatross: def swim(self): print("The albatross is swimming.") The two classes don’t share a common base class . Because they share the same interface, you can still use them in the same way: from birds import Duck, Albatross birds = [Duck(), Albatross()] for bird in birds: ... bird.swim() ... The duck is swimming. The albatross is swimming. class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Woof, Woof") class Cat(Animal): def speak(self): print("Meow, Meow") def make animal speak(animal): animal.speak() In this example, both Dog and Cat are subclasses of Animal and override the .speak() method. You can use instances of these classes the same way: The make animal speak() function demonstrates polymorphism by accepting any animal that inherits from Animal and calling its .speak() method, regardless of the specific type of animal. Learn how to define and use Python classes to implement object-oriented programming. Dive into attributes, methods, inheritance, and more.

Excerpt from a page describing this subject · 5,830 chars · not written by Vinony

Article · 日本語

ポリモーフィズム(英: polymorphism)とは、それぞれ異なる型に一元アクセスできる共通接点の提供、またはそれぞれ異なる型の多重定義を一括表現できる共通記号の提供を目的にした、型理論またはの概念および実装である。この用語は、有機組織および生物の種は様々な形態と段階を持つという生物学からの借用語である。多態性、多相性と邦訳されることが多い。 ポリモーフィズムは、通常以下の三種に分けられる。 * アドホック多相(ad hoc polymorphism)- 恣意的な型の集合に一つの共通接点を提供する。関数オーバーロード、Mix-inの一実装、型クラスなど。 * パラメトリック多相(parametric polymorphism)- 詳細化されていない型要素を内包する抽象的な型に記号表現を提供する。ジェネリクスや関数型言語の型構築子など。 * サブタイピング(subtyping)- サブタイプ多相(subtype polymorphism)やインクルージョン多相(inclusion polymorphism)とも。上位型をその下位型の数々で代替できるようにする。オブジェクト指向の多態性はこれを指す。 この他に、(row polymorphism)とポリタイピズム(polytypism)も挙げられることがある。対義語はモノモーフィズム(Monomorphism)である。

Abstract from DBpedia / Wikipedia · CC BY-SA

ポリモーフィズム · Vinony