「Java」インターフェース(interface)の引数ないメソッド(method)を実装するサンプル

2020年11月9日

構文
インターフェース名 オブジェクト名=new インターフェース名{
public void メソット名(){
//some code
}
}
1.インターフェース定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.cft;
//インターフェース
public interface EmpInfo {
//メソッド void
public void func();
}
package com.arkgame.study.cft; //インターフェース public interface EmpInfo { //メソッド void public void func(); }
package com.arkgame.study.cft;

//インターフェース
public interface EmpInfo {
      //メソッド void
      public void func();

}

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.cft;
public class InterFaceDemo {
public static void main(String[] args) {
EmpInfo emp = new EmpInfo() {
//interface メソッドを実装
public void func() {
System.out.println("this is a interface demo");
}
};
//インターフェースのメソッドを呼び出す
emp.func();
}
}
package com.arkgame.study.cft; public class InterFaceDemo { public static void main(String[] args) { EmpInfo emp = new EmpInfo() { //interface メソッドを実装 public void func() { System.out.println("this is a interface demo"); } }; //インターフェースのメソッドを呼び出す emp.func(); } }
package com.arkgame.study.cft;

public class InterFaceDemo {

      public static void main(String[] args) {
            EmpInfo emp = new EmpInfo() {
                  //interface メソッドを実装
                  public void func() {
                        System.out.println("this is a interface demo");
                  }
            };
            //インターフェースのメソッドを呼び出す
            emp.func();
      }

}

3.結果
this is a interface demo

Java

Posted by arkgame