Menu
Coddy logo textTech

Konstruktoren

Teil des Abschnitts Objektorientierte Programmierung der C#-Journey von Coddy. Lektion 7 von 70.

Ein Konstruktor ist eine spezielle Methode, die ausgeführt wird, wenn du ein Objekt erstellst. Er initialisiert die Eigenschaften und Felder des Objekts.

Standardkonstruktor ohne Parameter

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    
    // Standardkonstruktor
    public Book()
    {
        Title = "Unknown";
        Author = "Unknown";
    }
}

Parametrisierter Konstruktor

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    
    // Konstruktor mit Parametern
    public Book(string title, string author)
    {
        Title = title;
        Author = author;
    }
}

Überladen von Konstruktoren – mehrere Konstruktoren

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Pages { get; set; }
    
    // Konstruktor mit zwei Parametern
    public Book(string title, string author)
    {
        Title = title;
        Author = author;
        Pages = 0;
    }
    
    // Konstruktor mit drei Parametern
    public Book(string title, string author, int pages)
    {
        Title = title;
        Author = author;
        Pages = pages;
    }
}

Konstruktorverkettung mit this

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Pages { get; set; }
    
    // Hauptkonstruktor
    public Book(string title, string author, int pages)
    {
        Title = title;
        Author = author;
        Pages = pages;
    }
    
    // Ruft den Hauptkonstruktor mit Standardseiten auf
    public Book(string title, string author) : this(title, author, 0)
    {
    }
}

Objekte mit verschiedenen Konstruktoren erstellen

Book book1 = new Book("1984", "George Orwell");
Book book2 = new Book("Harry Potter", "J.K. Rowling", 500);

Konstruktoren haben denselben Namen wie die Klasse und keinen Rückgabetyp. Sie werden automatisch ausgeführt, wenn du das Schlüsselwort new verwendest. Du kannst mehrere Konstruktoren mit unterschiedlichen Parametern haben (Überladen).

challenge icon

Aufgabe

Mittel

Erstelle mithilfe von Konstruktorüberladung und -verkettung drei Konstruktoren für die Klasse Product:

  • Einen Konstruktor mit 3 Parametern: name, price und stock
  • Einen Konstruktor mit 2 Parametern: name und price (stock hat standardmäßig den Wert 0)
  • Einen Standardkonstruktor ohne Parameter (name = "Unknown", price = 0, stock = 0)

Verwende die Konstruktorverkettung mit this, um Code-Duplikation zu vermeiden.

Probier es selbst

using System;

class Program
{
    static void Main()
    {
        string name = Console.ReadLine();
        decimal price = decimal.Parse(Console.ReadLine());
        int stock = int.Parse(Console.ReadLine());
        
        Product product1 = new Product(name, price, stock);
        Product product2 = new Product();
        Product product3 = new Product("Keyboard", 79.99m);
        
        Console.WriteLine($"Product 1: {product1.Name} - ${product1.Price} (Stock: {product1.Stock})");
        Console.WriteLine($"Product 2: {product2.Name} - ${product2.Price} (Stock: {product2.Stock})");
        Console.WriteLine($"Product 3: {product3.Name} - ${product3.Price} (Stock: {product3.Stock})");
    }
}
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-C#-Compiler