By GokiSoft.com| 08:36 09/10/2021|
C Sharp

[Video] Tìm hiểu OOP C# - Tính chất đóng gói C# & Tính chất kế thừa C# - Override C# Overloading C# - C2009G

Link Video Bai Giang

Nội dung kiến thức:

I) Các tình chất trong lập trình OOP
	- Tính chất đóng gói:
		constructor
		access properties (public, private, protected, internal)
		getter/setter
	- Tính chất kế thừa:
		override
		overloading

Ví dụ Mini Project:
Xây dựng một chương trình phần mềm hỗ trợ quản lý thông công dân trong thành Hà nội:

Yêu cầu:
	- Công dân: Citizen
		- Thuộc tính:
			- tên -> string
			- tuổi -> int
			- địa chỉ -> string
			- giới tính -> string
			- cmtnd -> string
		- Phương thức / hành động
			- Input: nhập dữ liệu
			- Display: Hiển thị dữ liệu
			- Running
			- Sleeping
	- Sinh viên:
		- Thuộc tính:
			- tên
			- tuổi
			- địa chỉ
			- giới tính
			- cmtnd
			- email
			- rollno
		- Phương thức / hành động
			- Input: nhập dữ liệu
			- Display: Hiển thị dữ liệu
			- Running
			- Sleeping
	- Giáo viên:
		- Thuộc tính:
			- tên
			- tuổi
			- địa chỉ
			- giới tính
			- cmtnd
			- email
			- level -> bậc lương
		- Phương thức / hành động
			- Input: nhập dữ liệu
			- Display: Hiển thị dữ liệu
			- Running
			- Sleeping
Thực hiện chuyển đối tượng sau khi phân tích -> vào chương trình.




#Program.cs


using System;

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

        /**
         * Tim hieu T/c ke thua
         */
        static void Test03()
        {
            Student student = new Student();

            student.Input();

            student.Display();

            student.ShowMsg();
            student.ShowMsg("TRAN VAN A");
        }

        /**
         * Tim hieu getter/setter + constructor
         */
        static void Test02()
        {
            //Khoi tao doi tuong
            People people = new People();
            people.setFullname("Tran Van A");
            people.Address = "Ha Noi";
            //people.Gender = "Nam"; Gender chi getter khong co setter
            people.Cmtnd = "123456";
            people.Age = 20;

            Console.WriteLine("Ten: " + people.getFullname());
            Console.WriteLine("Dia chi: " + people.Address);
            Console.WriteLine("Gioi tinh: " + people.Gender);
            //Console.WriteLine("CMTND: " + people.Cmtnd);Cmtnd chi co setter & ko co getter
            Console.WriteLine("Tuoi: " + people.Age);

        }

        /**
         * T/c dong goi: Focus Citizen
         *      - Khai bao thuoc tinh + phuong thuc
         *      - Access properties: public | private
         *      - Su dung -> khoi tao object
         */
        static void Test01()
        {
            //Khoi tao doi tuong
            Citizen citizen1 = new Citizen();
            //citizen1.fullname = "TRAN VAN A"; internal -> ko goi dc trong class khac
            //citizen1.address = "Ha Noi"; private -> ko goi dc
            //citizen1.gender = "Ha Noi";protected -> ko goi dc trong class khac
            citizen1.age = 18;

            //Console.WriteLine("Ten: " + citizen1.fullname);internal -> ko goi dc trong class khac
            //Console.WriteLine("Dia chi: " + citizen1.address); private -> ko goi dc

            Citizen citizen2 = new Citizen("A", "Nam", "HN", "123", 20);
            //Console.WriteLine("Ten: " + citizen2.fullname);internal -> ko goi dc trong class khac
            //Console.WriteLine("Dia chi: " + citizen2.address); private -> ko goi dc

            //Key luan:
            //C# -> access properties: public | private
        }
    }
}


#Citizen.cs


using System;
namespace Lesson02
{
    public class Citizen
    {
        //Tin hieu: Access properties -> default | internal | friendly
        //access properties -> theo Java -> goi dc trong chinh class nay + cung package Java(namespace C#)
        //Thuc te vs C# thi sao???
        //Goi dc trong chinh doi nay -> OK
        //Goi dc trong class khac cung namespace hay ko??? -> Ko goi dc
        string fullname;

        //Tin hieu: Access properties -> protected???
        //Thuc te vs C# thi sao???
        //Goi dc trong chinh doi nay -> OK
        //Goi dc trong class khac cung namespace hay ko??? -> Ko goi dc
        protected string gender;

        //Tin hieu: Access properties -> private???
        //Thuc te vs C# thi sao???
        //Goi dc trong chinh doi nay -> OK
        //Goi dc trong class khac cung namespace hay ko??? -> Ko goi dc
        private string address;

        public string cmtnd;
        public int age;

        public Citizen()
        {
        }

        public Citizen(string fullname, string gender, string address, string cmtnd, int age)
        {
            this.fullname = fullname;
            this.gender = gender;
            this.address = address;
            this.cmtnd = cmtnd;
            this.age = age;
        }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin cong dan");
        }

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

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

        public void Sleeping()
        {
            Console.WriteLine("Sleepping...");
        }
    }
}


#People.cs


using System;
namespace Lesson02
{
    public class People
    {
        //Neu code getter/setter theo cach Java thi nhu sau START
        string fullname;
        public string getFullname()
        {
            return fullname;
        }
        public void setFullname(string fullname)
        {
            this.fullname = fullname;
        }
        //END
        //Trong C# -> khong su dung cach getter/setter cua Java

        //Cach tao C# nhu sau (getter/setter C#)
        public string Address { get; set; }

        //Chi tao getter va ko co setter -> quyen doc noi dung & ko quyen ghi
        public string Gender { get; private set; }

        //Chi co setter va khong co getter
        public string Cmtnd { private get; set; }

        //Trong TH: thuoc tinh age co dieu age >= 0. Xu ly getter/setter nhu the nao
        //public int Age { get; set; }
        private int _age;
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                if(value >= 0)
                {
                    this._age = value;
                } else
                {
                    Console.WriteLine("Nhap sai: Yeu cau age >= 0");
                }
            }
        }

        public People()
        {
        }

        public People(string fullname, string gender, string address, string cmtnd, int age)
        {
        }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin cong dan");
        }

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

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

        public void Sleeping()
        {
            Console.WriteLine("Sleepping...");
        }
    }
}


#Person.cs


using System;
namespace Lesson02
{
    public class Person
    {
        public string Fullname { get; set; }
        public string Address { get; set; }
        public string Gender { get; set; }
        public string Cmtnd { get; set; }
        private int _age;
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                if(value >= 0)
                {
                    this._age = value;
                } else
                {
                    Console.WriteLine("Yeu cau age > 0");
                }
            }
        }

        public Person()
        {
        }

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

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

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

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

            Console.WriteLine("Nhap Cmtnd: ");
            Cmtnd = 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 + ", gioi tinh: " + Gender +
                ", dia chi: " + Address + ", Cmtnd: " + Cmtnd +
                ", tuoi: " + Age;
        }

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

        public void Sleeping()
        {
            Console.WriteLine("Sleepping...");
        }
    }
}


#Student.cs


using System;
namespace Lesson02
{
    public class Student : Person
    {
        public string Rollno { get; set; }
        public string Email { get; set; }

        public Student()
        {
        }

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

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

            Console.WriteLine("Nhap MSV: ");
            Rollno = Console.ReadLine();

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

        //Overloading START
        public void ShowMsg()
        {
            Console.WriteLine("Hello World!!!");
        }

        public void ShowMsg(string msg)
        {
            Console.WriteLine("Hello > " + msg);
        }
        //Overloading END

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


Tags:

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

5

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