By GokiSoft.com| 19:23 26/06/2020|
C Sharp

[Share Code] Hướng dẫn lập trình OOP - Phần 1 - Lập trình C#, Lập Trình C Sharp

[Share Code] Hướng dẫn lập trình OOP - Phần 1 - Lập trình C#, Lập Trình C Sharp

Khái niệm về lập trình OOP
getter/setter
public/private/protected
Tính chất lập trình OOP
	- Tính bao đóng
	- Tính kế thừa
		- override
		- overloading
	- Tính đa hình
	- Tính trừu tượng
Interface

======================================
Xây dựng 1 chương trình quản lý máy tính:
	Thuộc tính của thằng máy tính: name, manufacturer, price, ram, cpu, monitor
	Phương thức cần quản lý
		- nhập thông tin
		- Hiển thị
 Chương trình quản lý xe cộ
 	- Car
 		Thuộc tính : name, age, color, seatNum, wheel
 		Thuộc tính
 			- chạy
 			- stop
 	- MotorBike
 		Thuộc tính : name, age, color, seatNum, wheel
 		Thuộc tính
 			- chạy
 			- stop


#Program.cs


using System;

namespace Lession2
{
    class Program
    {
        static void Main(string[] args)
        {
            Computer computer = new Computer();
            computer.setName("Xin Chao");//cach Java
            computer.Manufacturer = "Apple";//setter
            computer.Cpu = "Core i7";
            computer.Price = -12;

            Console.WriteLine("Name : " + computer.getName());
            Console.WriteLine("Price : " + computer.Price);
            //Console.WriteLine("Price : " + computer.Cpu);//Error => Cpu khong co quyen doc
            Console.WriteLine("Manufacturer : " + computer.Manufacturer);//getter

            Console.WriteLine("Hello World!");
        }
    }
}


#Computer.cs


using System;
namespace Lession2
{
    public class Computer
    {
        private string name;
        //Java => Theo phong cach Java
        public string getName() {
            return name;
        }

        public void setName(string name) {
            this.name = name;
        }
        //Cach Java => C# khong ap dung. => Cach nay chung ta khong dung cho chuong trinh C#

        //Cach 1: getter/setter => write & read
        public string Manufacturer { get; set; }

        //Cach 2: write only (CHỉ có quyền ghi và không có quyền đọc)
        public string Cpu { private get; set; }

        //Cach 3: read only (Chi co quyen doc va khong co quyen ghi)
        public string Ram { get; private set; }

        private float _price;
        public float Price {
            get {
                return _price;
            }

            set {
                if(value >= 0) {
                    _price = value;
                } else {
                    Console.WriteLine("Cai dat price sai");
                }
            }
        }

        public string monitor;

        public Computer()
        {
        }

        public void Display() {
            //code
        }

        public void Input() {
            //code.
        }
    }
}

Phần 2:


#Program.cs


using System;

namespace Lession2
{
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.Input();
        }
    }
}


#Vehicle.cs


using System;
namespace Lession2
{
    public class Vehicle
    {
        public string Name { get; set; }
        public string Color { get; set; }
        private int _wheel;
        public int Wheel {
            get{
                return _wheel;
            }
            set{
                if(value > 0) {
                    _wheel = value;
                } else {
                    Console.WriteLine("SO BANH KO DC AM");
                }
            }
        }

        public Vehicle()
        {
        }

        public Vehicle(string name, string color, int wheel) {
            Name = name;
            Color = color;
            Wheel = wheel;
        }

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

            Console.WriteLine("Nhap mau: ");
            Color = Console.ReadLine();

            Console.WriteLine("Nhap so banh: ");
            Wheel = int.Parse(Console.ReadLine());
        }

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

        public void Stop() {
            Console.WriteLine("Xe dang dung");
        }
    }
}


#MotorBike.cs


using System;
namespace Lession2
{
    public class MotorBike: Vehicle
    {
        public MotorBike()
        {
        }

        public MotorBike(string name, string color) : base(name, color, 2) {
        }
    }
}


#Car.cs


using System;
namespace Lession2
{
    public class Car : Vehicle
    {
        //fake them 1 thuoc tinh => hien chua nghi ra thuoc tinh khac.
        public string Title { get; set; }

        public Car()
        {
        }

        public Car(string name, string color) : base(name, color, 4) {
        }

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

            Console.WriteLine("Nhap tieu de: ");
            Title = Console.ReadLine();
        }
    }
}


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

5

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