「Java8」superで親クラスのコンストラクタを呼び出す

環境
JavaSE1.8
Eclipse 4.14

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class 子クラス名  extends 親クラス名 {
  public 子クラス名() {
super();
処理コード
}
public 子クラス名(データの型 引数) {
super(引数);
処理コード
}
public class 子クラス名  extends 親クラス名 {   public 子クラス名() { super(); 処理コード } public 子クラス名(データの型 引数) { super(引数); 処理コード }
public class 子クラス名  extends 親クラス名 {
 	public 子クラス名() {
            super();
            処理コード
      }
      public 子クラス名(データの型 引数) {
            super(引数);
            処理コード
      }

super で親クラスのコンストラクタを呼び出します。
使用例
1.親クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
//親クラス
public class Parent {
// 親クラスのコンストラクタ
public Parent() {
System.out.println("引数なしのコンストラクタ11");
}
// 引数1つ
public Parent(String target) {
System.out.println("文字列型ありコンストラクタ22" + target);
}
}
package com.arkgame.study; //親クラス public class Parent { // 親クラスのコンストラクタ public Parent() { System.out.println("引数なしのコンストラクタ11"); } // 引数1つ public Parent(String target) { System.out.println("文字列型ありコンストラクタ22" + target); } }
package com.arkgame.study;

//親クラス
public class Parent {

      // 親クラスのコンストラクタ
      public Parent() {
            System.out.println("引数なしのコンストラクタ11");
      }

      // 引数1つ
      public Parent(String target) {
            System.out.println("文字列型ありコンストラクタ22" + target);
      }
}

2.子クラスの定義
extendsで親クラスを継承しています。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
//子クラス 継承
public class Child extends Parent {
// 子クラスのコンストラクタ 引数なし
public Child() {
super();
System.out.println("AA");
}
// 子クラスのコンストラクタ 引数があり
public Child(String str) {
super(str);
System.out.println("BB");
}
}
package com.arkgame.study; //子クラス 継承 public class Child extends Parent { // 子クラスのコンストラクタ 引数なし public Child() { super(); System.out.println("AA"); } // 子クラスのコンストラクタ 引数があり public Child(String str) { super(str); System.out.println("BB"); } }
package com.arkgame.study;

//子クラス 継承
public class Child extends Parent {

      // 子クラスのコンストラクタ 引数なし
      public Child() {
            super();
            System.out.println("AA");
      }

      // 子クラスのコンストラクタ 引数があり
      public Child(String str) {
            super(str);
            System.out.println("BB");
      }

}

3.動作確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class Test {
public static void main(String[] args) {
// 子クラスのコンストラクタ(引数なし)を呼びだす
Child cd = new Child();
// 子クラスのコンストラクタ(引数あり)を呼び出す
Child cde = new Child("yamada");
}
}
package com.arkgame.study; public class Test { public static void main(String[] args) { // 子クラスのコンストラクタ(引数なし)を呼びだす Child cd = new Child(); // 子クラスのコンストラクタ(引数あり)を呼び出す Child cde = new Child("yamada"); } }
package com.arkgame.study;

public class Test {

      public static void main(String[] args) {
            // 子クラスのコンストラクタ(引数なし)を呼びだす
            Child cd = new Child();
            // 子クラスのコンストラクタ(引数あり)を呼び出す
            Child cde = new Child("yamada");

      }

}

実行結果
引数なしのコンストラクタ11
AA
文字列型ありコンストラクタ22yamada
BB

Java

Posted by arkgame