Menu
Coddy logo textTech

Konstruktor-Überladung

Teil des Abschnitts Objektorientierte Programmierung der Java-Journey von Coddy — Lektion 8 von 87.

Konstruktor-Überladung bedeutet, mehrere Konstruktoren mit unterschiedlichen Parameterlisten zu haben. Verwenden Sie this(), um einen Konstruktor von einem anderen aus aufzurufen.

Mehrere Konstruktoren

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;
    }
}

Konstruktor-Verkettung mit 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);  // Ruft den Konstruktor mit 3 Parametern auf
    }
    
    public Book() {
        this("Unknown", "Unknown");  // Ruft den Konstruktor mit 2 Parametern auf
    }
}

Verwendung

Book b1 = new Book("1984", "Orwell", 328);
Book b2 = new Book("Dune", "Herbert");
Book b3 = new Book();

this() muss die erste Anweisung im Konstruktor sein. Verkettung vermeidet die Duplizierung von Zuweisungslogik über Konstruktoren hinweg.

challenge icon

Aufgabe

Mittel

Erstellen Sie drei überladene Konstruktoren für die Product Klasse unter Verwendung von Konstruktor-Verkettung:

  • 3-Parameter-Konstruktor: name, price, stock
  • 2-Parameter-Konstruktor: name, price (stock ist standardmäßig 0)
  • Standardkonstruktor: keine Parameter (name = "Unknown", price = 0, stock = 0)

Verwenden Sie this(), um Konstruktoren zu verketten und Code-Duplizierung zu vermeiden.

Spickzettel

Das Überladen von Konstruktoren ermöglicht mehrere Konstruktoren mit unterschiedlichen Parameterlisten in derselben Klasse.

Beispiel für mehrere Konstruktoren:

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;
    }
}

Konstruktor-Verkettung mit this() ruft einen Konstruktor von einem anderen auf, um Code-Duplizierung zu vermeiden:

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);  // Ruft den Konstruktor mit 3 Parametern auf
    }
    
    public Book() {
        this("Unknown", "Unknown");  // Ruft den Konstruktor mit 2 Parametern auf
    }
}

Verwendung:

Book b1 = new Book("1984", "Orwell", 328);
Book b2 = new Book("Dune", "Herbert");
Book b3 = new Book();

Wichtig: this() muss die erste Anweisung im Konstruktor sein.

Probier es selbst

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() + ")");
    }
}
quiz iconTeste dich selbst

Diese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.

Alle Lektionen in Objektorientierte Programmierung