「Java」抽象インターフェース(abstract interface)を利用するサンプル
説明
インタフェース(interface):クラス仕様としての型定義をします。
抽象クラス(abstract):継承関係にあり、処理の再利用をします。
1.interfaceの定義
package com.arkgame.study;
public abstract interface DEP_CD {
/* development */
public static final String DEP = "1001";
/* design */
public static final String DES = "2002";
}
package com.arkgame.study;
public abstract interface DEP_CD {
/* development */
public static final String DEP = "1001";
/* design */
public static final String DES = "2002";
}
package com.arkgame.study; public abstract interface DEP_CD { /* development */ public static final String DEP = "1001"; /* design */ public static final String DES = "2002"; }
2.Javaコード
package com.arkgame.study;
//Empクラスの定義
class Emp {
String name;
String code;
public Emp(String name, String code) {
this.name = name;
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
public class AbstractInterfaceDemo {
public static void main(String[] args) {
//クラスのオブジェクト生成
Emp emp1 = new Emp("Employee A", "1001");
Emp emp2 = new Emp("Employee B", "2002");
//インターフェースを利用
if (DEP_CD.DEP.equals(emp1.getCode())) {
System.out.println(emp1.getName() + " is 開発部のメンバー");
}
if (DEP_CD.DES.equals(emp2.getCode())) {
System.out.println(emp2.getName() + " is 企画部のメンバー ");
}
}
}
package com.arkgame.study;
//Empクラスの定義
class Emp {
String name;
String code;
public Emp(String name, String code) {
this.name = name;
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
public class AbstractInterfaceDemo {
public static void main(String[] args) {
//クラスのオブジェクト生成
Emp emp1 = new Emp("Employee A", "1001");
Emp emp2 = new Emp("Employee B", "2002");
//インターフェースを利用
if (DEP_CD.DEP.equals(emp1.getCode())) {
System.out.println(emp1.getName() + " is 開発部のメンバー");
}
if (DEP_CD.DES.equals(emp2.getCode())) {
System.out.println(emp2.getName() + " is 企画部のメンバー ");
}
}
}
package com.arkgame.study; //Empクラスの定義 class Emp { String name; String code; public Emp(String name, String code) { this.name = name; this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } } public class AbstractInterfaceDemo { public static void main(String[] args) { //クラスのオブジェクト生成 Emp emp1 = new Emp("Employee A", "1001"); Emp emp2 = new Emp("Employee B", "2002"); //インターフェースを利用 if (DEP_CD.DEP.equals(emp1.getCode())) { System.out.println(emp1.getName() + " is 開発部のメンバー"); } if (DEP_CD.DES.equals(emp2.getCode())) { System.out.println(emp2.getName() + " is 企画部のメンバー "); } } }
実行結果
Employee A is 開発部のメンバー
Employee B is 企画部のメンバー