「Java」this()でクラスのコンストラクタを呼ぶサンプル

2021年4月7日

書式
public クラス名(引数1,引数2){
this(引数1)
javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.study.arkgame;
public class SampleDemo {
String username;
int age;
private static final String splitStr = "*****************";
// 引数なし コンストラクタ
SampleDemo() {
this.username = "user001";
this.age = 32;
}
// 引数1個 コンストラクタ
SampleDemo(String username) {
// コンストラクタを呼ぶ
this();
this.username = username;
}
// 引数2個 今スト楽
public SampleDemo(String username, int age) {
this(username);
this.age = age;
}
public static void main(String[] args) {
// 引数なし
SampleDemo sa = new SampleDemo();
System.out.println("名前1: " + sa.username);
System.out.println("年齢1: " + sa.age);
System.out.println(splitStr);
// 引数 1個
SampleDemo sb = new SampleDemo("User002");
System.out.println("名前2: " + sb.username);
System.out.println("年齢2: " + sb.age);
System.out.println(splitStr);
// 引数 2個
SampleDemo sc = new SampleDemo("User003", 40);
System.out.println("名前3: " + sc.username);
System.out.println("年齢3: " + sc.age);
}
}
package com.study.arkgame; public class SampleDemo { String username; int age; private static final String splitStr = "*****************"; // 引数なし コンストラクタ SampleDemo() { this.username = "user001"; this.age = 32; } // 引数1個 コンストラクタ SampleDemo(String username) { // コンストラクタを呼ぶ this(); this.username = username; } // 引数2個 今スト楽 public SampleDemo(String username, int age) { this(username); this.age = age; } public static void main(String[] args) { // 引数なし SampleDemo sa = new SampleDemo(); System.out.println("名前1: " + sa.username); System.out.println("年齢1: " + sa.age); System.out.println(splitStr); // 引数 1個 SampleDemo sb = new SampleDemo("User002"); System.out.println("名前2: " + sb.username); System.out.println("年齢2: " + sb.age); System.out.println(splitStr); // 引数 2個 SampleDemo sc = new SampleDemo("User003", 40); System.out.println("名前3: " + sc.username); System.out.println("年齢3: " + sc.age); } }
package com.study.arkgame;

public class SampleDemo {
      String username;
      int age;

      private static final String splitStr = "*****************";

      // 引数なし コンストラクタ
      SampleDemo() {
            this.username = "user001";
            this.age = 32;
      }

      // 引数1個 コンストラクタ
      SampleDemo(String username) {
            // コンストラクタを呼ぶ
            this();
            this.username = username;
      }

      // 引数2個 今スト楽
      public SampleDemo(String username, int age) {
            this(username);
            this.age = age;
      }

      public static void main(String[] args) {

            // 引数なし
            SampleDemo sa = new SampleDemo();
            System.out.println("名前1: " + sa.username);
            System.out.println("年齢1: " + sa.age);
            System.out.println(splitStr);
            // 引数 1個
            SampleDemo sb = new SampleDemo("User002");
            System.out.println("名前2: " + sb.username);
            System.out.println("年齢2: " + sb.age);
            System.out.println(splitStr);
            // 引数 2個
            SampleDemo sc = new SampleDemo("User003", 40);
            System.out.println("名前3: " + sc.username);
            System.out.println("年齢3: " + sc.age);

      }

}

実行結果
名前1: user001
年齢1: 32
*****************
名前2: User002
年齢2: 32
*****************
名前3: User003
年齢3: 40

Java

Posted by arkgame