By GokiSoft.com| 15:59 05/10/2021|
C Sharp

[Video] Tìm hiểu lập trình OOP trong C# - Tính chất đóng gói - tính chất kế thừa - Override trong C# - C2010L

Link Video Bài Giảng



Nôi dung buổi học:
Lập trình hướng đối tượng & T/c lập trình OOP
- T/c đóng gói
	access properties (public/private/protected/internal (friendly - default))
- T/c kế thừa:
	override
	overloading

Mini Project: Xây dựng dự án quản lý thông tin công dân, sinh viên, giáo viên, ... trong thành phố hà nội:

Phân tích:
	Công dân -> Citizen:
		Thuộc tính:
			- fullname -> string
			- age -> int
			- cmtnd -> string
			- address -> string
			- gender -> string
		Phương thức/hành động:
			- Input -> nhập dữ liệu
			- Display -> Hiển thị dữ liệu
			- Running ...
	Sinh viên -> Student
		Thuộc tính:
			- fullname -> string
			- age -> int
			- cmtnd -> string
			- address -> string
			- gender -> string
			- Rollno -> string
			- Email -> string
		Phương thức/hành động:
			- Input -> nhập dữ liệu
			- Display -> Hiển thị dữ liệu
			- Running ...
	Giáo viên -> Teacher
		Thuộc tính:
			- fullname -> string
			- age -> int
			- cmtnd -> string
			- address -> string
			- gender -> string
			- email -> string
			- level -> int
		Phương thức/hành động:
			- Input -> nhập dữ liệu
			- Display -> Hiển thị dữ liệu
			- Running ...

	-> Triển khai lớp đối tượng -> khai báo biến & method trong class object
	-> Thực khởi tạo đối tượng từ class object
	-> Tìm hiểu về public/private/protected/internal (default) -> C#
	-> Tìm hiểu cách tạo getter/setter trong C#
	-> Tìm hiểu cách tạo nhiều constructor trong C#

	-> T/c kế thừa -> triển khai như nào
	-> overloading.




#Program.cs


using System;

namespace Lesson02.Models
{
    class Program
    {
        static void Main(string[] args)
        {
            //Test01();
            //Test02();
            Test03();
        }

        static void Test03()
        {
            Student student = new Student("a@gmail.com", "R001", "A", "123", "Nam", "Ha Noi", 18);
            student.Display();

            Student student1 = new Student();
            student1.Input();
            student1.Display();

            student1.TestMsg();
            student1.TestMsg("ABC");
        }

        static void Test02()
        {
            //Tìm hiểu cách tạo getter/setter trong C#
            Citizen citizen = new Citizen();
            //Cach Java
            citizen.setFullname("Tran Van A");
            Console.WriteLine("Ten: " + citizen.getFullname());
            //Cach C# => getter/setter
            citizen.Cmtnd = "123123123";
            Console.WriteLine("Ten: " + citizen.Cmtnd);
            //Cach C# -> chi co getter
            //citizen.Address = "Ha Noi";//setter -> error
            Console.WriteLine("Dia chi: " + citizen.Address);//getter -> OK

            //Cach C# -> chi co setter
            citizen.Gender = "Nam";//setter -> OK
            //Console.WriteLine("Gioi tinh: " + citizen.Gender);//getter -> error

            citizen.Age = -10;
            citizen.Age = 10;
            Console.WriteLine("Tuoi: " + citizen.Age);

            Citizen citizen2 = new Citizen("A", "123", "Nam", "Ha Noi", 12);
            citizen2.Display();
        }

        static void Test01()
        {
            //-> Thực khởi tạo đối tượng từ class object
            Citizen01 citizen = new Citizen01();
            citizen.Display();

            //-> Tìm hiểu về public/private/protected/internal (default) -> C#
            //internal -> ko goi dc trong class object khac (cung namespace hoac khac namespace)
            //private -> ko goi dc trong class object khac (cung namespace hoac khac namespace)
            //protected -> ko goi dc trong class object khac (cung namespace hoac khac namespace)
            //public -> goi dc trong class object (cung namespace ...)
            citizen.gender = "Nam";
            Console.WriteLine("Gioi tinh: " + citizen.gender);
        }
    }
}


#Teacher.cs


using System;
namespace Lesson02.Models
{
    public class Teacher : Citizen02
    {
        public string Email { get; set; }
        public int Level { get; set; }

        public Teacher()
        {
        }
    }
}


#Student.cs


using System;
namespace Lesson02.Models
{
    public class Student : Citizen02
    {
        public string Email { get; set; }
        public string RollNo { get; set; }

        public Student()
        {
        }

        public Student(string email, string rollno,
            string fullname, string cmtnd,
            string gender, string address,
            int age):base(fullname, cmtnd, gender, address, age)
        {
            Email = email;
            RollNo = rollno;
        }

        public override void Input()
        {
            base.Input();

            Console.WriteLine("Nhap email: ");
            Email = Console.ReadLine();

            Console.WriteLine("Nhap RollNo: ");
            RollNo = Console.ReadLine();
        }

        public void TestMsg()
        {
            Console.WriteLine("Test ...");
        }

        public void TestMsg(string msg)
        {
            Console.WriteLine("Test ... " + msg);
        }

        public override string ToString()
        {
            return base.ToString() + ", Email: " + Email + ", RollNo: " + RollNo;
        }
    }
}


#Citizen02.cs


using System;
namespace Lesson02.Models
{
    public class Citizen02
    {
        public string Fullname { get; set; }
        public string Cmtnd { get; set; }
        public string Address { get; set; }
        public string Gender { get; set; }

        public int _age;
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                if(value < 0)
                {
                    Console.WriteLine("Yeu cau age >= 0");
                } else
                {
                    _age = value;
                }
            }
        }

        public Citizen02()
        {
        }

        public Citizen02(string fullname, string cmtnd, string gender, string address, int age)
        {
            Fullname = fullname;
            Cmtnd = cmtnd;
            Gender = gender;
            Address = address;
            Age = age;
        }

        public virtual void Input()
        {
            Console.WriteLine("Nhap ten: ");
            Fullname = Console.ReadLine();

            Console.WriteLine("Nhap CMTND: ");
            Cmtnd = Console.ReadLine();

            Console.WriteLine("Nhap gioi tinh: ");
            Gender = Console.ReadLine();

            Console.WriteLine("Nhap dia chi: ");
            Address = Console.ReadLine();

            Console.WriteLine("Nhap tuoi: ");
            Age = int.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine(this);
            //this -> ToString()
        }

        public override string ToString()
        {
            return "Ten: " + Fullname + ", CMTND: " + Cmtnd + ", Gioi Tinh: " + Gender +
                ", Dia chi: " + Address + ", Tuoi: " + Age;
        }

        public void Running()
        {
            Console.WriteLine("Dang chay ...");
        }
    }
}


#Citizen01.cs


using System;
namespace Lesson02.Models
{
    public class Citizen01
    {
        private string fullname;
        protected string cmtnd;
        string address;
        public string gender;
        int age;

        public Citizen01()
        {
        }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin du lieu");
        }

        public void Display()
        {
            Console.WriteLine("Hien thi thong tin du lieu");
        }

        public void Running()
        {
            Console.WriteLine("Dang chay ...");
        }
    }
}


#Citizen.cs


using System;
namespace Lesson02.Models
{
    public class Citizen
    {
        string fullname;
        //Vi du: tao getter/setter theo cach cua Java.
        public string getFullname()
        {
            return fullname;
        }
        public void setFullname(string fullname)
        {
            this.fullname = fullname;
        }
        //getter/setter -> Java END
        //Chu y: C# khong su dung cach cua Java -> lam theo cach khac.
        //C# -> tao getter/setter nhu the nao???
        public string Cmtnd { get; set; }

        //thuoc tinh address -> cai getter & ko cai setter
        public string Address { get; private set; }

        //thuoc tinh gender -> cai setter & ko co getter
        public string Gender { private get; set; }

        //Test phan validate du lieu: yeu cau age >= 0
        //public int Age { get; set; }
        //Code theo cach khac
        private int _age;//bien private
        //Trien khai getter/setter
        public int Age
        {
            get
            {
                //Chung ta the su dung cau lenh sau khong
                //Console.WriteLine("Testing getter age ...");
                //return Age; => Bien thanh de quy vo han
                return _age;
            }

            set
            {
                //kiem tra va set du lieu nhu the nao???
                //value -> keyword -> vi du: citizen.Age = -10; -> value = -10
                if(value < 0)
                {
                    Console.WriteLine("Yeu cau age >= 0");
                    return;
                }
                this._age = value;
            }
        }

        public Citizen()
        {
        }

        //Tìm hiểu cách tạo nhiều constructor trong C#
        public Citizen(string fullname, string cmtnd, string gender, string address, int age)
        {
            setFullname(fullname);
            Cmtnd = cmtnd;
            Gender = gender;
            Address = address;
            Age = age;
        }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin du lieu");
        }

        public void Display()
        {
            Console.WriteLine("Hien thi thong tin du lieu");
        }

        public void Running()
        {
            Console.WriteLine("Dang chay ...");
        }
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)