Menu
Coddy logo textTech

Überladen von Konstruktoren

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

Konstruktorenverkettung 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

Erstelle drei überladene Konstruktoren für die Klasse Product mithilfe einer Konstruktorverkettung:

  • Konstruktor mit 3 Parametern: name, price, stock
  • Konstruktor mit 2 Parametern: name, price (stock wird standardmäßig auf 0 gesetzt)
  • Standardkonstruktor: keine Parameter (name = "Unknown", price = 0, stock = 0)

Verwende this(), um Konstruktoren zu verketten und Code-Duplikation zu vermeiden.

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

Übe selbstständig: Online-Java-Compiler