28. インターフェイス
28. インターフェイス
インターフェイス
lesson 069
class Lesson069 { public static void main(String args[]) { LOVE_HINA obj = new LOVE_HINA(); obj.write("祝福のときは来る、手を伸ばして"); } } interface intf { void write(String str); } class LOVE_HINA implements intf { public void write(String str) { System.out.println(str); } }
実行結果
L:\>java Lesson069
祝福のときは来る、手を伸ばして
複数のインターフェイスを実装
lesson 070
class Lesson070 { public static void main(String args[]) { LOVE_HINA obj = new LOVE_HINA(); obj.write("名前\t\t年齢"); obj.hina("前原しのぶ" , 13); } } interface intf1 { void write(String str); } interface intf2 { void hina(String name , int age); } class super_class { String name; int age; } class LOVE_HINA extends super_class implements intf1 , intf2 { public void write(String str) { System.out.println(str); } public void hina(String name , int age) { this.name = name; this.age = age; System.out.println(this.name + "\t" + this.age); } }
実行結果
L:\>java Lesson070
名前 年齢
前原しのぶ 13
インターフェイスで宣言された変数はデフォルトで public static final
lesson 071
class Lesson071 { public static void main(String args[]) { LOVE_HINA naru = new LOVE_HINA(); System.out.println("名前\t\t年齢\t血液型"); naru.write(); System.out.println("\t" + NARU.blood_type); } } interface NARU { String name = "成瀬川なる"; int age = 17; char blood_type = 'A'; void write(); } class LOVE_HINA implements NARU{ String name = NARU.name; int age = NARU.age; public void write() { System.out.print(this.name + "\t" + this.age); } }
実行結果
L:\>java Lesson071
名前 年齢 血液型
成瀬川なる 17 A
インターフェイスの継承
lesson 072
class Lesson072 { public static void main(String args[]) { EVA ev = new EVA(); ev.write("初号機、完全に沈黙!パイロットの反応がありません" , 23); ev.write(ev.third); } } interface A { void write(String str , int synchro); } interface B { String third = "逃げちゃだめだ"; } interface C extends A , B { void write(String str); } class EVA implements C { public void write(String str , int synchro) { System.out.println(str); System.out.println("シンクロ率 : " + synchro + "%"); } public void write(String str) { for (int count = 0 ; count <= 5 ; count++) System.out.println(str); } }
実行結果
L:\>java Lesson072
初号機、完全に沈黙!パイロットの反応がありません
シンクロ率 : 23%
逃げちゃだめだ
逃げちゃだめだ
逃げちゃだめだ
逃げちゃだめだ
逃げちゃだめだ
逃げちゃだめだ