Abstraction

Abstraction — bu obyektning faqat muhim xususiyatlarini ajratib ko‘rsatish va keraksiz tafsilotlarni yashirish tamoyili. C# dasturlash tilida abstraksiya abstract class va interface yordamida amalga oshiriladi. 1 Abstract class yordamida abstraksiya // Abstrakt klass yaratamiz public abstract class Vehicle { public abstract void Start(); // Abstrakt metod - tanasiz public void Stop() { Console.WriteLine("Mashina to‘xtadi."); } } // "Car" klassi "Vehicle" dan meros oladi public class Car : Vehicle { // Abstrakt metodni amalga oshiramiz public override void Start() { Console.WriteLine("Mashina ishga tushdi."); } } class Program { static void Main() { Car myCar = new Car(); myCar.Start(); // Abstrakt metod myCar.Stop(); // Oddiy metod } } 2 Interface yordamida abstraksiya // Abstrakt interface public interface IVehicle { void Start(); // Faqat metod e’lon qilinadi void Stop(); } // Car interface ni implement qiladi public class Car : IVehicle { public void Start() { Console.WriteLine("Mashina ishga tushdi."); } public void Stop() { Console.WriteLine("Mashina to‘xtadi."); } } class Program { static void Main() { IVehicle myCar = new Car(); myCar.Start(); myCar.Stop(); } } Xullosa Abstraction yordamida faqat muhim xususiyatlar ochiq bo‘ladi, ichki tafsilotlar yashiriladi. Abstract class yoki Interface yordamida amalga oshiriladi. Abstract class faqat bitta ota klassdan meros olishi mumkin. Interface bir nechta sinflar tomonidan implement qilinishi mumkin.

Apr 2, 2025 - 16:02
 0
Abstraction

Abstraction — bu obyektning faqat muhim xususiyatlarini ajratib ko‘rsatish va keraksiz tafsilotlarni yashirish tamoyili.
C# dasturlash tilida abstraksiya abstract class va interface yordamida amalga oshiriladi.

1 Abstract class yordamida abstraksiya

// Abstrakt klass yaratamiz
public abstract class Vehicle
{
    public abstract void Start(); // Abstrakt metod - tanasiz

    public void Stop()
    {
        Console.WriteLine("Mashina to‘xtadi.");
    }
}

// "Car" klassi "Vehicle" dan meros oladi
public class Car : Vehicle
{
    // Abstrakt metodni amalga oshiramiz
    public override void Start()
    {
        Console.WriteLine("Mashina ishga tushdi.");
    }
}

class Program
{
    static void Main()
    {
        Car myCar = new Car();
        myCar.Start(); // Abstrakt metod
        myCar.Stop();  // Oddiy metod
    }
}

2 Interface yordamida abstraksiya

// Abstrakt interface
public interface IVehicle
{
    void Start(); // Faqat metod e’lon qilinadi
    void Stop();
}

// Car interface ni implement qiladi
public class Car : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Mashina ishga tushdi.");
    }

    public void Stop()
    {
        Console.WriteLine("Mashina to‘xtadi.");
    }
}

class Program
{
    static void Main()
    {
        IVehicle myCar = new Car();
        myCar.Start();
        myCar.Stop();
    }
}

Xullosa
Abstraction yordamida faqat muhim xususiyatlar ochiq bo‘ladi, ichki tafsilotlar yashiriladi.
Abstract class yoki Interface yordamida amalga oshiriladi.
Abstract class faqat bitta ota klassdan meros olishi mumkin.
Interface bir nechta sinflar tomonidan implement qilinishi mumkin.