Menu
CoddyTech

Konstruktor

Termasuk bagian Pemrograman Berorientasi Objek dalam perjalanan C# di Coddy. Pelajaran 7 dari 70.

Constructor adalah method khusus yang dijalankan saat kamu membuat object. Constructor menginisialisasi property dan field milik object tersebut.

Konstruktor default tanpa parameter

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

Konstruktor berparameter

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

Overloading constructor, beberapa constructor

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Pages { get; set; }
    
    // Konstruktor dengan dua parameter
    public Book(string title, string author)
    {
        Title = title;
        Author = author;
        Pages = 0;
    }
    
    // Konstruktor dengan tiga parameter
    public Book(string title, string author, int pages)
    {
        Title = title;
        Author = author;
        Pages = pages;
    }
}

Perantaian konstruktor dengan this

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Pages { get; set; }
    
    // Konstruktor utama
    public Book(string title, string author, int pages)
    {
        Title = title;
        Author = author;
        Pages = pages;
    }
    
    // Memanggil konstruktor utama dengan nilai default untuk pages
    public Book(string title, string author) : this(title, author, 0)
    {
    }
}

Membuat object dengan constructor yang berbeda

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

Constructor memiliki nama yang sama dengan class dan tidak memiliki tipe pengembalian. Constructor berjalan secara otomatis saat kamu menggunakan kata kunci new. Kamu bisa memiliki beberapa constructor dengan parameter yang berbeda (overloading).

challenge icon

Tantangan

Sedang

Buat tiga constructor untuk class Product menggunakan overloading dan chaining constructor:

  • Constructor dengan 3 parameter: name, price, dan stock
  • Constructor dengan 2 parameter: name dan price (stock secara default bernilai 0)
  • Constructor default tanpa parameter (name = "Unknown", price = 0, stock = 0)

Gunakan chaining constructor dengan this agar tidak ada duplikasi kode.

Coba sendiri

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 iconUji dirimu

Pelajaran ini berisi kuis singkat. Mulai pelajarannya untuk menjawab kuis dan melacak progresmu.

Semua pelajaran di Pemrograman Berorientasi Objek

Latihan sendiri: Compiler C# online