Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lập trình hướng đối tượng (OOP)

Class và Object

public class Person
{
    // Fields
    private string _name;
    private int _age;
    
    // Properties
    public string Name
    {
        get => _name;
        set => _name = value;
    }
    
    public int Age
    {
        get => _age;
        set
        {
            if (value >= 0)
                _age = value;
        }
    }
    
    // Auto-property
    public string Email { get; set; }
    
    // Constructor
    public Person(string name, int age)
    {
        _name = name;
        _age = age;
    }
    
    // Method
    public void Introduce()
    {
        Console.WriteLine($"Hi, I'm {_name}, {_age} years old.");
    }
}

// Sử dụng
var person = new Person("Alice", 30);
person.Introduce();

Inheritance (Kế thừa)

public class Animal
{
    public string Name { get; set; }
    
    public virtual void MakeSound()
    {
        Console.WriteLine("Some sound");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
    
    public void Fetch()
    {
        Console.WriteLine("Fetching ball...");
    }
}

// Sử dụng
Animal myDog = new Dog();
myDog.MakeSound(); // "Woof!"

Polymorphism (Đa hình)

public abstract class Shape
{
    public abstract double CalculateArea();
}

public class Circle : Shape
{
    public double Radius { get; set; }
    
    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }
    
    public override double CalculateArea()
    {
        return Width * Height;
    }
}

// Sử dụng đa hình
List<Shape> shapes = new List<Shape>
{
    new Circle { Radius = 5 },
    new Rectangle { Width = 4, Height = 6 }
};

foreach (var shape in shapes)
{
    Console.WriteLine($"Area: {shape.CalculateArea()}");
}

Encapsulation (Đóng gói)

public class BankAccount
{
    // Private field - encapsulated
    private decimal _balance;
    
    // Public property với validation
    public decimal Balance
    {
        get => _balance;
        private set
        {
            if (value >= 0)
                _balance = value;
        }
    }
    
    public void Deposit(decimal amount)
    {
        if (amount > 0)
            Balance += amount;
    }
    
    public bool Withdraw(decimal amount)
    {
        if (amount > 0 && amount <= Balance)
        {
            Balance -= amount;
            return true;
        }
        return false;
    }
}

Interface

public interface ILogger
{
    void Log(string message);
    void LogError(string error);
}

public interface IDisposable
{
    void Dispose();
}

public class FileLogger : ILogger, IDisposable
{
    public void Log(string message)
    {
        File.AppendAllText("log.txt", $"{DateTime.Now}: {message}\n");
    }
    
    public void LogError(string error)
    {
        Log($"ERROR: {error}");
    }
    
    public void Dispose()
    {
        // Cleanup resources
    }
}

Abstract Class vs Interface

FeatureAbstract ClassInterface
Constructor✅ Có❌ Không
Fields✅ Có❌ Không (chỉ properties)
Method implementation✅ Có (có thể có cả abstract và concrete)❌ Không (C# 8+ có default implementation)
Multiple inheritance❌ Không✅ Có
Access modifiers✅ Có (public, protected, private)❌ Mặc định public