OOP (Object-Oriented Programming) cơ bản (Phần 1)

OOP là gì?

Class

C#
public class Student
{
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
        Student studentDuy = new Student();
        Debug.Log(studentDuy);
    }
}

Property

C#
public class Student
{
    public string Name;
    public int Age;
    public string Major;
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
        Student studentDuy = new Student();
        studentDuy.Name = "Duy";
        studentDuy.Age = 18;
        studentDuy.Major = "Game Development";
        Debug.Log($"Name: {studentDuy.Name}, Age: {studentDuy.Age}, Major: {studentDuy.Major}");
    }
}

Constructor

C#
public class Student
{
    public string Name;
    public int Age;
    public string Major;

    public Student(string name, int age, string major)
    {
        Name = name;
        Age = age;
        Major = major;
    }
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
				// ...

        Student studentHuy = new Student("Huy", 20, "Game Development");
        Debug.Log($"Name: {studentHuy.Name}, Age: {studentHuy.Age}, Major: {studentHuy.Major}");
    }
}

Default Constructor

C#
public class Student
{
    public string Name;
    public int Age;
    public string Major;

    public Student()
    {
        Name = "Anonymous";
        Age = -1;
        Major = "None";
    }

    public Student(string name, int age, string major)
    {
        Name = name;
        Age = age;
        Major = major;
    }
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
        // ...

        Student defaultStudent = new Student();
        Debug.Log($"Name: {defaultStudent.Name}, Age: {defaultStudent.Age}, Major: {defaultStudent.Major}");
    }
}

Method

C#
public class Student
{
    public string Name;
    public int Age;
    public string Major;

    public Student()
    {
        Name = "Anonymous";
        Age = -1;
        Major = "None";
    }

    public Student(string name, int age, string major)
    {
        Name = name;
        Age = age;
        Major = major;
    }

    public void Introduce()
    {
        Debug.Log("Hello, my name is " + Name + ", I'm " + Age + " years old, and I'm studying " + Major + ".");
    }
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
        // ...

        Student studentAn = new Student
        {
            Name = "An",
            Age = 19,
            Major = "Game Design"
        };
        studentAn.Introduce();Ï
    }
}

Access modifier: public và private

C#
public class Student
{
    public string Name;
    private int _age;
    public string Major;
    
    public string GetAge()
    {
        int ageDividedBy10 = _age / 10;
        return $"U{(ageDividedBy10 + 1) * 10}";
    }

    public Student()
    {
        Name = "Anonymous";
        _age = -1;
        Major = "None";
    }

    public Student(string name, int age, string major)
    {
        Name = name;
        _age = age;
        Major = major;
    }

    public void Introduce()
    {
        Debug.Log(
            "Hello, my name is " + Name + ", I'm " + GetAge() + ", and I'm studying " + Major + "."
        );
    }
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
		    Student studentDuy = new Student("Duy", 18, "Game Development");
        studentDuy.Introduce();
    }
}

Lưu ý: Biến private nên được đặt tên theo camelCase với prefix “_” và biến public nên được đặt tên theo PascalCase theo convention của Microsoft.

Best practice là dùng private variable cùng với getters và setters

C#
public class Student
{
    private string _name;
    private int _age;
    private string _major;

    public string GetName()
    {
        return _name;
    }

    public void SetName(string name)
    {
        _name = name;
    }

    public void SetAge(int age)
    {
        _age = age;
    }

    public string GetAge()
    {
        int ageDividedBy10 = _age / 10;
        return $"U{(ageDividedBy10 + 1) * 10}";
    }

    public string GetMajor()
    {
        return _major;
    }

    public void SetMajor(string major)
    {
        _major = major;
    }

    public Student()
    {
        _name = "Anonymous";
        _age = -1;
        _major = "None";
    }

    public Student(string name, int age, string major)
    {
        _name = name;
        _age = age;
        _major = major;
    }

    public void Introduce()
    {
        Debug.Log(
            "Hello, my name is " + _name + ", I'm " + GetAge() + ", and I'm studying " + _major + "."
        );
    }
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
				Student studentHuy = new Student("Huy", 20, "Game Development");
        Debug.Log($"Name: {studentHuy.GetName()}, Age: {studentHuy.GetAge()}, Major: {studentHuy.GetMajor()}");
    }
}

Dùng auto properties cho getters và setters

C#
public class Student
{
    public string Name { get; set; }
    private int _age;
    public string Major { get; set; }

    public string Age
    {
        get
        {
            int ageDividedBy10 = _age / 10;
            return $"U{(ageDividedBy10 + 1) * 10}";
        }
        set => _age = int.Parse(value);
    }

    public Student()
    {
        Name = "Anonymous";
        _age = -1;
        Major = "None";
    }

    public Student(string name, int age, string major)
    {
        Name = name;
        _age = age;
        Major = major;
    }

    public void Introduce()
    {
        Debug.Log(
            "Hello, my name is " + Name + ", I'm " + Age + ", and I'm studying " + Major + "."
        );
    }
}
C#
public class TestStudent : MonoBehaviour
{
    private void Start()
    {
				Student studentHuy = new Student("Huy", 20, "Game Development");
        Debug.Log($"Name: {studentHuy.Name}, Age: {studentHuy.Age}, Major: {studentHuy.Major}");
    }
}