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.

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

Parametresiz varsayılan yapıcı

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

Constructor overloading - birden fazla yapıcı metot

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

this ile yapıcı (constructor) zincirleme

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

Farklı constructor'lar ile nesne oluşturma

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

Yapıcılar (Constructors), sınıfla aynı isme sahiptir ve dönüş türü içermezler. new anahtar kelimesini kullandığınızda otomatik olarak çalışırlar. Farklı parametrelere sahip birden fazla yapıcıya sahip olabilirsiniz (aşırı yükleme - overloading).

challenge icon

Görev

Orta

Yapıcı aşırı yükleme (constructor overloading) ve zincirleme (chaining) kullanarak Product sınıfı için üç adet yapıcı metot oluşturun:

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

Kod tekrarını önlemek için this anahtar kelimesiyle yapıcı zincirleme (constructor chaining) 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