By GokiSoft.com|
10:09 01/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# - C2010G
Lập trình hướng đối tương (OOP)
- Tích chất bao đóng
- Tích chất kế thừa
- Override
- Overloading
- Tích chất đa hình
- Tích chất trừu tượng
- Tìm hiểu interface
======================================================
Nội dung buổi học:
Lập trình hướng đối tương (OOP)
- Tích chất bao đóng
- Tích chất kế thừa
- Override
- Overloading
List -> Khai bảo 1 mảng động.
======================================================
Mini Project: Thiết kế chương trình quản lý thiết bị cửa bán đồ điện tử
- Device:
- Thược tính:
name: string
manufacturerName: string
madein: string
importedDate: string
- Phương thức:
Input: Nhập dữ liệu
Display: Hiển thị dữ liệu
- Laptop
- Thuộc tính:
name: string
manufacturerName: string
madein: string
size: string
importedDate: string
cpu: string
- Phương thức & hành động
Input: Nhập dữ liệu
Display: Hiển thị dữ liệu
- Bàn phím
- Thuộc tính:
name: string
manufacturerName: string
madein: string
importedDate: string
- Phương thức & hành động
Input: Nhập dữ liệu
Display: Hiển thị dữ liệu
- Khai báo các đối tượng trên -> gọi tới hàm Input & Display
- Khai báo 1 mảng quản lý dánh sách laptop
#Laptop.cs
using System;
namespace Lesson02
{
public class Laptop : Device
{
public string Size { get; set; }
public string Cpu { get; set; }
public Laptop()
{
}
public Laptop(string size, string cpu,
string name, string manufacturerName,
string madein, string importedDate):base(name, manufacturerName, madein, importedDate)
{
Size = size;
Cpu = cpu;
}
public override void Input()
{
base.Input();
Console.WriteLine("Nhap kich co man hinh: ");
Size = Console.ReadLine();
Console.WriteLine("Nhap CPU: ");
Cpu = Console.ReadLine();
}
public override string ToString()
{
return "Kich co man hinh: " + Size + ", CPU: " + Cpu + ", " + base.ToString();
}
//public override void Display()
//{
// Console.WriteLine("ok");
//}
}
}
#Device.cs
using System;
namespace Lesson02
{
public class Device
{
//access properties -> public, protected, private, internal -> Khac so vs Java.
//Thuoc tinh -> default
//Tim hieu ve getter/setter trong C#
//Nhin lai cach thiet ke getter/setter trong Java
string name;
public string getName()
{
return this.name;
}
public void setName(string name)
{
this.name = name;
}
//stop -> khong su dung getter/setter theo cach cua Java
//Cach trien khai getter/setter theo cach C#
//Hoc cach trien khai phu hop -> theo C# -> OK
public string ManufacturerName { get; set; }
//Chi co thuoc tinh lay du lieu, ko cai dc du lieu -> class khac
public string MadeIn { get; private set; }
public string ImportedDate { private get; set; }
//public int Amount { get; set; }//so luong san pham trong kho
//Trong TH -> kiem tra du lieu var -> khi setter
private int _amount;
public int Amount
{
get
{
return this._amount;
}
set
{
if(value < 0)
{
Console.WriteLine("Yeu cau nhap amount >= 0");
return;
}
this._amount = value;
}
}
public Device()
{
}
public Device(string name, string manufacturerName, string madein, string importedDate)
{
this.name = name;
this.ManufacturerName = manufacturerName;
this.MadeIn = madein;
this.ImportedDate = importedDate;
}
public virtual void Input()
{
Console.WriteLine("Nhap ten: ");
name = Console.ReadLine();
Console.WriteLine("NSX: ");
ManufacturerName = Console.ReadLine();
Console.WriteLine("Xuat su: ");
MadeIn = Console.ReadLine();
Console.WriteLine("Ngay nhap: ");
ImportedDate = Console.ReadLine();
}
public virtual void Display()
{
Console.WriteLine(this);
}
public override string ToString()
{
return "Ten: " + name + ", NSX: " + ManufacturerName + ", Xuat su: " + MadeIn + ", Ngay nhap: " + ImportedDate;
}
}
}
#Program.cs
using System;
namespace Lesson02
{
class Program
{
static void Main(string[] args)
{
Device device = new Device();
//device.name = "Dell Vostro Y450";
//device.manufacturerName = "Dell";
//device.madein = "China";
//device.importedDate = "2021-09-20";
device.setName("Dell Vostro Y450");
device.ManufacturerName = "Dell";
//device.MadeIn = "China"; -> error -> ko co setter
device.ImportedDate = "2021-09-20";
device.Amount = -10;
device.Input();
device.Display();
Console.WriteLine("Ten thiet bi: " + device.getName());
Console.WriteLine("NSX: " + device.ManufacturerName);
Console.WriteLine("MadeIn: " + device.MadeIn);
//Console.WriteLine("Imported Date: " + device.ImportedDate); -> error -> ko co getter
Console.WriteLine("So luong: " + device.Amount);
//Test ke thua
Laptop laptop = new Laptop();
laptop.ManufacturerName = "Viet Nam";
laptop.Cpu = "Core i7";
Console.WriteLine("NSX: {0}, Cpu: {1}", laptop.ManufacturerName, laptop.Cpu);
laptop.Input();
laptop.Display();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)