「Java」instanceof演算子でインターフェースを実装するかどうかを判定するサンプル

構文
オブジェクトの変数名 instanceof インターフェース名
使用例
1.インタフェースの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java;
//interface definition
public abstract interface EmpParentInter {
// method definition
public abstract boolean isAuthUser();
}
package com.arkgame.study.java; //interface definition public abstract interface EmpParentInter { // method definition public abstract boolean isAuthUser(); }
package com.arkgame.study.java;

//interface definition
public abstract interface EmpParentInter {

      // method definition
      public abstract boolean isAuthUser();
}

2.親クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java;
// class definition
public class EmpParentDemo implements EmpParentInter {
String empId;
Integer age;
// parent constructor
public EmpParentDemo(String empId, Integer age) {
this.empId = empId;
this.age = age;
}
@Override
public boolean isAuthUser() {
String res = "admin";
if (("admin").equals(res))
return true;
return false;
}
}
package com.arkgame.study.java; // class definition public class EmpParentDemo implements EmpParentInter { String empId; Integer age; // parent constructor public EmpParentDemo(String empId, Integer age) { this.empId = empId; this.age = age; } @Override public boolean isAuthUser() { String res = "admin"; if (("admin").equals(res)) return true; return false; } }
package com.arkgame.study.java;

// class definition
public class EmpParentDemo implements EmpParentInter {

      String empId;
      Integer age;

      // parent constructor
      public EmpParentDemo(String empId, Integer age) {
            this.empId = empId;
            this.age = age;
      }

      @Override
      public boolean isAuthUser() {
            String res = "admin";
            if (("admin").equals(res))
                  return true;
            return false;
      }

}

3.子クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java;
// child class extends
public class EmpChildDemo extends EmpParentDemo {
String depId;
// child class constructor
public EmpChildDemo(String empId, Integer age, String depId) {
super(empId, age);
this.depId = depId;
}
}
package com.arkgame.study.java; // child class extends public class EmpChildDemo extends EmpParentDemo { String depId; // child class constructor public EmpChildDemo(String empId, Integer age, String depId) { super(empId, age); this.depId = depId; } }
package com.arkgame.study.java;

// child class extends
public class EmpChildDemo extends EmpParentDemo {

      String depId;

      // child class constructor
      public EmpChildDemo(String empId, Integer age, String depId) {
            super(empId, age);
            this.depId = depId;
      }

}

4.実行確認(main)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java;
public class EmpSuperMethod {
public static void main(String[] args) {
// child object statement
EmpChildDemo empChildObj = new EmpChildDemo("A001", 32, "AB");
// instanceof 子クラス
boolean resA = empChildObj instanceof EmpChildDemo;
// instanceof 親クラス
boolean resB = empChildObj instanceof EmpParentDemo;
// instanceof インタフェース
boolean resC = empChildObj instanceof EmpParentInter;
System.out.println("子クラス(sub class)のインスタンス:\n " + resA);
System.out.println("親クラス(super class)のインスタンス:\n " + resB);
System.out.println("インタフェース(interface)のインスタンス:\n " + resC);
}
}
package com.arkgame.study.java; public class EmpSuperMethod { public static void main(String[] args) { // child object statement EmpChildDemo empChildObj = new EmpChildDemo("A001", 32, "AB"); // instanceof 子クラス boolean resA = empChildObj instanceof EmpChildDemo; // instanceof 親クラス boolean resB = empChildObj instanceof EmpParentDemo; // instanceof インタフェース boolean resC = empChildObj instanceof EmpParentInter; System.out.println("子クラス(sub class)のインスタンス:\n " + resA); System.out.println("親クラス(super class)のインスタンス:\n " + resB); System.out.println("インタフェース(interface)のインスタンス:\n " + resC); } }
package com.arkgame.study.java;

public class EmpSuperMethod {

      public static void main(String[] args) {

            // child object statement
            EmpChildDemo empChildObj = new EmpChildDemo("A001", 32, "AB");
            // instanceof 子クラス
            boolean resA = empChildObj instanceof EmpChildDemo;
            // instanceof 親クラス
            boolean resB = empChildObj instanceof EmpParentDemo;
            // instanceof インタフェース
            boolean resC = empChildObj instanceof EmpParentInter;

            System.out.println("子クラス(sub class)のインスタンス:\n " + resA);
            System.out.println("親クラス(super class)のインスタンス:\n " + resB);
            System.out.println("インタフェース(interface)のインスタンス:\n " + resC);

      }

}

5.実行結果

子クラス(sub class)のインスタンス:
true
親クラス(super class)のインスタンス:
true
インタフェース(interface)のインスタンス:
true

Java

Posted by arkgame