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.

Kopya kağıdı

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

Yapıcı metotlar sınıf ile aynı ada sahiptir ve dönüş türleri yoktur. new anahtar kelimesini kullandığınızda otomatik olarak çalışırlar.

Parametresiz varsayılan yapıcı metot (default constructor):

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

Parametreli yapıcı metot (parameterized constructor):

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

Yapıcı metot aşırı yüklemesi (constructor overloading) - farklı parametrelere sahip birden fazla yapıcı metot:

public class Book
{
    public string Title { get; set; }
    public int Pages { get; set; }
    
    public Book(string title)
    {
        Title = title;
        Pages = 0;
    }
    
    public Book(string title, int pages)
    {
        Title = title;
        Pages = pages;
    }
}

Kod tekrarını önlemek için this ile yapıcı metot zincirleme (constructor chaining):

public class Book
{
    public string Title { get; set; }
    public int Pages { get; set; }
    
    public Book(string title, int pages)
    {
        Title = title;
        Pages = pages;
    }
    
    public Book(string title) : this(title, 0)
    {
    }
}

Farklı yapıcı metotlarla nesne oluşturma:

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

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