第14章 多態性
多態性に関する基本的な考え方について説明します
homepage
# **多態性** *** 多態性とは「多形態」を意味し、継承によって互いに関連するクラスが多数ある場合に発生します。 たとえば、名前はAnimalというスーパークラスがanimalSound()があります。動物のサブクラスには、豚、猫、犬、鳥などがあります。また、動物のサウンドを独自に実装することもできます(豚の鳴き声、猫の鳴き声など)。 <pre><code>class Animal { public void animalSound() { System.out.println("The animal makes a sound"); } } class Pig extends Animal { public void animalSound() { System.out.println("The pig says: wee wee"); } } class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bow wow"); } } class MyMainClass { public static void main(String[] args) { Animal myAnimal = new Animal(); // Create a Animal object Animal myPig = new Pig(); // Create a Pig object Animal myDog = new Dog(); // Create a Dog object myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); } } </code></pre> #### **実行結果** The animal makes a sound<br> The pig says: wee wee<br> The dog says: bow wow <br><br> **「継承」と「多態性」を使用する理由** > コードの再利用性に役立ちます。新しいクラスを作成するときに、既存のクラスの属性とメソッドを再利用します。 #### **例** [JSample14_1.java] ``` package JSample; public class JSample14_1 { public static void main(String[] args) { show(new Cat()); show(new Dog()); Animal a = new Cat(); a.eat(); Cat c = (Cat)a; c.work(); } public static void show(Animal a) { a.eat(); if (a instanceof Cat) { Cat c = (Cat)a; c.work(); } else if (a instanceof Dog) { Dog c = (Dog)a; c.work(); } } } abstract class Animal { abstract void eat(); } class Cat extends Animal { public void eat() { System.out.println("Catに食べる?"); } public void work() { System.out.println("Catにネズミを捕まえる"); } } class Dog extends Animal { public void eat() { System.out.println("Dogに食べる?"); } public void work() { System.out.println("Dogに留守番する"); } } ``` #### **実行結果** <pre> <iframe src="javascript:'<html> Catに食べる?<br> Catにネズミを捕まえる<br> Dogに食べる?<br> Dogに留守番する<br> Catに食べる?<br> Catにネズミを捕まえる </html>'" width="300" height="200"></iframe> </pre> <br> **多態性を実現する方法** + 継承 + 抽象クラス + インターフェース <br> + **インターフェース** インターフェースとは、クラスに含まれるメソッドの具体的な処理内容を記述せず、変数とメソッドの型のみを定義したものです 以下のようにインターフェースを宣言および実装を行います。 ``` //インターフェースを宣言 interface インターフェース名{} ``` ``` //インターフェースを実装 class クラス名 implements インターフェース名{} ``` #### **例** [JSample14_2.java] ``` public class JSample14_2 { public static void main(String[] args) { ControlBikeIf myBike = new BikeGo(); myBike.start(); myBike.speedup(); myBike.stop(); myBike.start(); } } ``` [ControlBikeIf.java] ``` public interface ControlBikeIf { void start(); void stop(); void speedup(); void speeddown(); } ``` [BikeGo.java] ``` public class BikeGo implements ControlBikeIf { public void start() { System.out.println("自電車起動"); } public void stop() { System.out.println("自電車停止"); } public void speedup() { System.out.println("速度アップ"); } public void speeddown() { System.out.println("速度ダウン"); } } ``` #### **実行結果** 自電車起動<br> 速度アップ<br> 自電車停止<br> 自電車起動 <br><br> *** > **練習** >> **問題1**[JEx14_1.java] 下記のプログラムを完成ください。 ``` package JSample; class SuperC { public void sc() { System.out.println("This is SuperC!"); } } class Subc1 extends SuperC { public void sc() { System.out.println("This is Subc1!"); } } class Subc2 extends SuperC { public void sc() { System.out.println("This is Subc2!"); } } public class Ex14_1 { public static void main(String[] args) { SuperC a; ______①__________ ______②__________ a = b; a.sc(); a = c; a.sc(); } } ``` #### **実行結果** <pre> <iframe src="javascript:'<html> This is Subc1!<br> This is Subc2! </html>'"></iframe> </pre>
content
戻る