「Java」抽象インターフェース(abstract interface)と抽象メソット(abstract method)を利用する方法
構文
1.クラスの定義
public abstract interface クラス名 {
}
2.メソットの定義
public abstract 戻り型 関数名()
3.インターフェースの実装
public class 実装クラス名 implements 抽象インターフェース
使用例
1.抽象インターフェース(abstract interface)と抽象メソッド(abstract method)の定義
package com.arkgame.study.java;
//sbatract interface
public abstract interface ParentInterface {
// abstract method definition
public abstract String getInfo(Object cft);
}
package com.arkgame.study.java;
//sbatract interface
public abstract interface ParentInterface {
// abstract method definition
public abstract String getInfo(Object cft);
}
package com.arkgame.study.java; //sbatract interface public abstract interface ParentInterface { // abstract method definition public abstract String getInfo(Object cft); }
2.抽象インターフェースを実装するコード
package com.arkgame.study.java;
//implements Parent Interface
public class ChildImp implements ParentInterface {
// getInfo method override
@Override
public String getInfo(Object cft) {
String od = "old home:";
String result = null;
// instance of
if (cft != null) {
if (cft instanceof String) {
result = od + (String) cft;
} else if (cft instanceof Integer) {
result = od + cft.toString();
} else if (cft instanceof Boolean) {
result = od + cft.toString();
}
}
return result;
}
}
package com.arkgame.study.java;
//implements Parent Interface
public class ChildImp implements ParentInterface {
// getInfo method override
@Override
public String getInfo(Object cft) {
String od = "old home:";
String result = null;
// instance of
if (cft != null) {
if (cft instanceof String) {
result = od + (String) cft;
} else if (cft instanceof Integer) {
result = od + cft.toString();
} else if (cft instanceof Boolean) {
result = od + cft.toString();
}
}
return result;
}
}
package com.arkgame.study.java; //implements Parent Interface public class ChildImp implements ParentInterface { // getInfo method override @Override public String getInfo(Object cft) { String od = "old home:"; String result = null; // instance of if (cft != null) { if (cft instanceof String) { result = od + (String) cft; } else if (cft instanceof Integer) { result = od + cft.toString(); } else if (cft instanceof Boolean) { result = od + cft.toString(); } } return result; } }
3.インターフェースと抽象クラスを利用する確認クラス
package com.arkgame.study.java;
public class InterCftDemo {
public static void main(String[] args) {
String resA;
String resB;
String resC;
ChildImp cd = new ChildImp();
// string object
resA = cd.getInfo("ToTo");
// Integer object
resB = cd.getInfo(123);
// Boolean object
resC = cd.getInfo(true);
System.out.println("String型object: " + resA);
System.out.println("Integer型object: " + resB);
System.out.println("Boolean型object: " + resC);
}
}
package com.arkgame.study.java;
public class InterCftDemo {
public static void main(String[] args) {
String resA;
String resB;
String resC;
ChildImp cd = new ChildImp();
// string object
resA = cd.getInfo("ToTo");
// Integer object
resB = cd.getInfo(123);
// Boolean object
resC = cd.getInfo(true);
System.out.println("String型object: " + resA);
System.out.println("Integer型object: " + resB);
System.out.println("Boolean型object: " + resC);
}
}
package com.arkgame.study.java; public class InterCftDemo { public static void main(String[] args) { String resA; String resB; String resC; ChildImp cd = new ChildImp(); // string object resA = cd.getInfo("ToTo"); // Integer object resB = cd.getInfo(123); // Boolean object resC = cd.getInfo(true); System.out.println("String型object: " + resA); System.out.println("Integer型object: " + resB); System.out.println("Boolean型object: " + resC); } }
4.実行結果
String型object: old home:ToTo
Integer型object: old home:123
Boolean型object: old home:true
String型object: old home:ToTo
Integer型object: old home:123
Boolean型object: old home:true
String型object: old home:ToTo Integer型object: old home:123 Boolean型object: old home:true