「Java入門」インターフェース多態性(ポリモフィズム)を使うサンプル

2021年1月6日

書式
メソッド名(インタフェースの型 変数名)
変数名:クラスのインスタンス
使用例
1.インターフェースの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public interface InterDemo {
// 抽象メソッド
void testFunc();
}
package com.arkgame.study; public interface InterDemo { // 抽象メソッド void testFunc(); }
package com.arkgame.study;

public interface InterDemo {
      // 抽象メソッド
      void testFunc();
}

2.インターフェースを実装

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.util.ArrayList;
// TestAクラスでインターフェースを実装
class TestA implements InterDemo {
@Override
public void testFunc() {
System.out.println("class TestA 1001");
}
}
//TestBクラスでインターフェースを実装
class TestB implements InterDemo {
@Override
public void testFunc() {
System.out.println("class TestB 2002");
}
}
//TestCクラスでインターフェースを実装
class TestC implements InterDemo {
@Override
public void testFunc() {
System.out.println("class TestC 3003");
}
}
public class PolymorphismDemo {
// 共通の関数を呼び出す
static void RunInterFunc(InterDemo id) {
id.testFunc();
}
public static void main(String[] args) {
// インスタンスをリストに追加
ArrayList<InterDemo> interLst = new ArrayList<>();
interLst.add(new TestA());
interLst.add(new TestB());
interLst.add(new TestC());
//リストの要素(インターフェース)をループする
for (InterDemo kk : interLst) {
RunInterFunc(kk);
}
}
}
package com.arkgame.study; import java.util.ArrayList; // TestAクラスでインターフェースを実装 class TestA implements InterDemo { @Override public void testFunc() { System.out.println("class TestA 1001"); } } //TestBクラスでインターフェースを実装 class TestB implements InterDemo { @Override public void testFunc() { System.out.println("class TestB 2002"); } } //TestCクラスでインターフェースを実装 class TestC implements InterDemo { @Override public void testFunc() { System.out.println("class TestC 3003"); } } public class PolymorphismDemo { // 共通の関数を呼び出す static void RunInterFunc(InterDemo id) { id.testFunc(); } public static void main(String[] args) { // インスタンスをリストに追加 ArrayList<InterDemo> interLst = new ArrayList<>(); interLst.add(new TestA()); interLst.add(new TestB()); interLst.add(new TestC()); //リストの要素(インターフェース)をループする for (InterDemo kk : interLst) { RunInterFunc(kk); } } }
package com.arkgame.study;

import java.util.ArrayList;

// TestAクラスでインターフェースを実装
class TestA implements InterDemo {
      @Override
      public void testFunc() {
            System.out.println("class TestA 1001");
      }
}

//TestBクラスでインターフェースを実装
class TestB implements InterDemo {
      @Override
      public void testFunc() {
            System.out.println("class TestB 2002");
      }
}

//TestCクラスでインターフェースを実装
class TestC implements InterDemo {
      @Override
      public void testFunc() {
            System.out.println("class TestC 3003");
      }
}

public class PolymorphismDemo {

      // 共通の関数を呼び出す
      static void RunInterFunc(InterDemo id) {
            id.testFunc();

      }

      public static void main(String[] args) {
      
          // インスタンスをリストに追加
            ArrayList<InterDemo> interLst = new ArrayList<>();
            interLst.add(new TestA());
            interLst.add(new TestB());
            interLst.add(new TestC());

            //リストの要素(インターフェース)をループする
            for (InterDemo kk : interLst) {
                  RunInterFunc(kk);
            }

      }

}

3.実行結果
class TestA 1001
class TestB 2002
class TestC 3003

Java

Posted by arkgame