Menu
Coddy logo textTech

Yapıcı Metotlar

Coddy'nin C# Journey'sinin Nesne Yönelimli Programlama bölümünün bir parçası. Ders 7 / 70.

Yapıcı, bir nesne oluşturduğunuzda çalışan özel bir metottur. Nesnenin özelliklerini ve alanlarını başlatır.

Parametresiz varsayılan oluşturucu

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    
    // Varsayılan yapıcı
    public Book()
    {
        Title = "Unknown";
        Author = "Unknown";
    }
}

Parametreli yapıcı

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    
    // Parametreli yapıcı
    public Book(string title, string author)
    {
        Title = title;
        Author = author;
    }
}

Yapıcı aşırı yükleme - birden fazla yapıcı

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Pages { get; set; }
    
    // İki parametreli yapıcı
    public Book(string title, string author)
    {
        Title = title;
        Author = author;
        Pages = 0;
    }
    
    // Üç parametreli yapıcı
    public Book(string title, string author, int pages)
    {
        Title = title;
        Author = author;
        Pages = pages;
    }
}

this ile kurucu zincirleme

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Pages { get; set; }
    
    // Ana yapıcı
    public Book(string title, string author, int pages)
    {
        Title = title;
        Author = author;
        Pages = pages;
    }
    
    // Varsayılan sayfalarla ana yapıcıyı çağırır
    public Book(string title, string author) : this(title, author, 0)
    {
    }
}

farklı oluşturucularla nesneler oluşturma

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

Yapıcılar sınıfla aynı ada sahiptir ve dönüş türleri yoktur. new anahtar sözcüğünü kullandığınızda otomatik olarak çalışırlar. Farklı parametrelere sahip birden fazla yapıcıya sahip olabilirsiniz (aşırı yükleme).

challenge icon

Görev

Orta

Yapıcı aşırı yükleme ve zincirleme kullanarak Product sınıfı için üç yapıcı oluşturun:

  • 3 parametreli bir yapıcı: name, price ve stock
  • 2 parametreli bir yapıcı: name ve price (stock varsayılan olarak 0 olur)
  • Parametresiz bir varsayılan yapıcı (name = "Unknown", price = 0, stock = 0)

Kod tekrarını önlemek için this ile yapıcı zincirlemesini kullanın.

Kendin dene

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 iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Nesne Yönelimli Programlama bölümündeki tüm dersler

Kendi başına pratik yap: Online C# derleyicisi