「Java」super()でスーパークラスのコンストラクタと関数を継承する方法

2020年11月30日

1.親クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.mvc;
public abstract class BaseInfo {
protected String userName = "TestUser_Parent";
// コンストラクター
public BaseInfo(String userName) {
this.userName = userName;
}
// カスタマイズ関数
public int update(int x, int y) {
return x + y;
}
}
package com.arkgame.study.mvc; public abstract class BaseInfo { protected String userName = "TestUser_Parent"; // コンストラクター public BaseInfo(String userName) { this.userName = userName; } // カスタマイズ関数 public int update(int x, int y) { return x + y; } }
package com.arkgame.study.mvc;

public abstract class BaseInfo {

      protected String userName = "TestUser_Parent";

      // コンストラクター
      public BaseInfo(String userName) {
            this.userName = userName;
      }

      // カスタマイズ関数
      public int update(int x, int y) {
            return x + y;
      }
}

2.子クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.mvc;
public class Childcust extends BaseInfo {
// 親のコンストラクタを継承
public Childcust(String userName) {
super("TestUser_child");
}
// 親クラスの関数を継承
public int childFunc(int i, int j) {
return super.update(i, j);
}
}
package com.arkgame.study.mvc; public class Childcust extends BaseInfo { // 親のコンストラクタを継承 public Childcust(String userName) { super("TestUser_child"); } // 親クラスの関数を継承 public int childFunc(int i, int j) { return super.update(i, j); } }
package com.arkgame.study.mvc;

public class Childcust extends BaseInfo {

      // 親のコンストラクタを継承
      public Childcust(String userName) {
            super("TestUser_child");
      }

      // 親クラスの関数を継承
      public int childFunc(int i, int j) {
            return super.update(i, j);
      }
}

3.確認クラス

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.mvc;
public class ChildcustDemo {
public static void main(String[] args) {
// 子クラスのオブジェクト作成
Childcust cft = new Childcust("main test");
// 子クラスの関数を呼び出す
int result = cft.childFunc(5, 9);
System.out.println("test result: " + result);
// クラスのメンバー変数
System.out.println("メンバー変数結果:" + cft.userName);
}
}
package com.arkgame.study.mvc; public class ChildcustDemo { public static void main(String[] args) { // 子クラスのオブジェクト作成 Childcust cft = new Childcust("main test"); // 子クラスの関数を呼び出す int result = cft.childFunc(5, 9); System.out.println("test result: " + result); // クラスのメンバー変数 System.out.println("メンバー変数結果:" + cft.userName); } }
package com.arkgame.study.mvc;

public class ChildcustDemo {

      public static void main(String[] args) {
            // 子クラスのオブジェクト作成
            Childcust cft = new Childcust("main test");
            // 子クラスの関数を呼び出す
            int result = cft.childFunc(5, 9);
            System.out.println("test result: " + result);
            // クラスのメンバー変数
            System.out.println("メンバー変数結果:" + cft.userName);
      }

}

4.結果
test result: 14
メンバー変数結果:TestUser_child

Java

Posted by arkgame