コンストラクタのオーバーロード
CoddyのJavaジャーニー「オブジェクト指向プログラミング」セクションの一部 — レッスン 8/87。
コンストラクタのオーバーロードとは、異なる引数リストを持つ複数のコンストラクタを持つことを意味します。あるコンストラクタから別のコンストラクタを呼び出すには、this() を使用します。
複数のコンストラクタ
public class Book {
private String title;
private String author;
private int pages;
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public Book(String title, String author) {
this.title = title;
this.author = author;
this.pages = 0;
}
}this() を使用したコンストラクタ・チェイニング
public class Book {
private String title;
private String author;
private int pages;
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public Book(String title, String author) {
this(title, author, 0); // 3引数のコンストラクタを呼び出す
}
public Book() {
this("Unknown", "Unknown"); // 2引数のコンストラクタを呼び出す
}
}使い方
Book b1 = new Book("1984", "Orwell", 328);
Book b2 = new Book("Dune", "Herbert");
Book b3 = new Book();this() はコンストラクタ内の最初のステートメントである必要があります。チェイニングにより、複数のコンストラクタ間で代入ロジックが重複するのを避けることができます。
チャレンジ
中級コンストラクタチェーンを使用して、Productクラスに3つのオーバーロードされたコンストラクタを作成してください:
- 3引数コンストラクタ:name、price、stock
- 2引数コンストラクタ:name、price(stockのデフォルト値は0)
- デフォルトコンストラクタ:引数なし(name = "Unknown"、price = 0、stock = 0)
this()を使用してコンストラクタをチェーンさせ、コードの重複を避けてください。
チートシート
コンストラクタのオーバーロードにより、同じクラス内に異なるパラメータリストを持つ複数のコンストラクタを定義できます。
複数のコンストラクタの例:
public class Book {
private String title;
private String author;
private int pages;
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public Book(String title, String author) {
this.title = title;
this.author = author;
this.pages = 0;
}
}this() を使用したコンストラクタチェーンは、コードの重複を避けるために、あるコンストラクタから別のコンストラクタを呼び出します:
public class Book {
private String title;
private String author;
private int pages;
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public Book(String title, String author) {
this(title, author, 0); // 3引数のコンストラクタを呼び出す
}
public Book() {
this("Unknown", "Unknown"); // 2引数のコンストラクタを呼び出す
}
}使用例:
Book b1 = new Book("1984", "Orwell", 328);
Book b2 = new Book("Dune", "Herbert");
Book b3 = new Book();重要: this() はコンストラクタ内の最初のステートメントである必要があります。
自分で試してみよう
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
double price = Double.parseDouble(scanner.nextLine());
int stock = Integer.parseInt(scanner.nextLine());
Product product1 = new Product(name, price, stock);
Product product2 = new Product("Keyboard", 79.99);
Product product3 = new Product();
System.out.println("Product 1: " + product1.getName() + " - $" + product1.getPrice() + " (Stock: " + product1.getStock() + ")");
System.out.println("Product 2: " + product2.getName() + " - $" + product2.getPrice() + " (Stock: " + product2.getStock() + ")");
System.out.println("Product 3: " + product3.getName() + " - $" + product3.getPrice() + " (Stock: " + product3.getStock() + ")");
}
}このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
オブジェクト指向プログラミングのすべてのレッスン
4継承
継承の基本 (extends)super キーワードメソッドのオーバーライド (@Override)コンストラクタチェーンObject クラス単一継承と多段階継承なぜ多重継承ができないのかまとめ:従業員の階層構造