Tạo lớp trừu tượng Shape:
Thuộc tính:
public string Color { get; set; }
Phương thức:
public abstract double GetArea();
public abstract double GetPerimeter();
public virtual void PrintInfo() in ra loại hình, màu, diện tích, chu vi.
Gợi ý: Cấu trúc kế thừa Shape tương tự ví dụ Shape -> Rectangle -> Square trong bài tập C# về inheritance
public abstract class Shape
{
public string Color { get; set; }
protected Shape(string color)
{
Color = color;
}
public abstract double GetArea();
public abstract double GetPerimeter();
public virtual void PrintInfo()
{
Console.WriteLine($"Loai: {GetType().Name}, Mau: {Color}, " +
$"Dien tich: {GetArea():0.00}, Chu vi: {GetPerimeter():0.00}");
}
}
Tạo các lớp kế thừa:
Circle : Shape
Thuộc tính: Radius
Constructor nhận color, radius
Cài đặt GetArea(), GetPerimeter().
Rectangle : Shape
Thuộc tính: Width, Height
Constructor nhận color, width, height.
Square : Rectangle
Chỉ nhận color, side
Không cho phép lớp khác kế thừa Square (dùng sealed).
public class Circle : Shape
{
public double Radius { get; set; }
public Circle(string color, double radius) : base(color)
{
Radius = radius;
}
public override double GetArea() => Math.PI * Radius * Radius;
public override double GetPerimeter() => 2 * Math.PI * Radius;
}
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public Rectangle(string color, double width, double height) : base(color)
{
Width = width;
Height = height;
}
public override double GetArea() => Width * Height;
public override double GetPerimeter() => 2 * (Width + Height);
}
public sealed class Square : Rectangle
{
public double Side => Width;
public Square(string color, double side) : base(color, side, side)
{
}
public override void PrintInfo()
{
Console.WriteLine($"Loai: {GetType().Name}, Mau: {Color}, " +
$"Canh: {Side}, Dien tich: {GetArea():0.00}, Chu vi: {GetPerimeter():0.00}");
}
}
Trong Main:
Tạo List<Shape> chứa nhiều Circle, Rectangle, Square.
Duyệt danh sách, gọi PrintInfo().
Tính tổng diện tích tất cả hình.
static void Main(string[] args)
{
List<Shape> shapes = new List<Shape>
{
new Circle("Red", 3),
new Rectangle("Blue", 4, 5),
new Square("Green", 6)
};
double totalArea = 0;
foreach (var s in shapes)
{
s.PrintInfo();
totalArea += s.GetArea();
}
Console.WriteLine($"Tong dien tich: {totalArea:0.00}");
Console.ReadLine();
}
Bài này mô phỏng “quản lý nhân sự” kế thừa từ lớp người, giống hướng dẫn kế thừa Nguoi -> NhanVien, KhachHang trong C#.
Thuộc tính:
Id, Name, Gender, BirthDate
Phương thức:
virtual void Input()
virtual void Display()
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public DateTime BirthDate { get; set; }
public virtual void Input()
{
Console.Write("Nhap ID: ");
Id = Console.ReadLine();
Console.Write("Nhap ho ten: ");
Name = Console.ReadLine();
Console.Write("Nhap gioi tinh: ");
Gender = Console.ReadLine();
Console.Write("Nhap ngay sinh (yyyy-MM-dd): ");
BirthDate = DateTime.Parse(Console.ReadLine());
}
public virtual void Display()
{
Console.WriteLine($"ID: {Id}, Ten: {Name}, Gioi tinh: {Gender}, " +
$"Ngay sinh: {BirthDate:yyyy-MM-dd}");
}
}
Employee : Person
Thuộc tính: Degree, BaseSalary
Ghi đè Input(), Display() để nhập/hiển thị thêm Degree, BaseSalary (dùng base.Input()), tương tự ví dụ override Nhap, HienThi trong bài VietJack.
Phương thức ảo public virtual double CalcSalary().
OfficeEmployee : Employee
Thuộc tính: OvertimeHours
Lương = BaseSalary + OvertimeHours * 50000.
SalesEmployee : Employee
Thuộc tính: SalesAmount
Lương = BaseSalary + SalesAmount * 0.05.
public class Employee : Person
{
public string Degree { get; set; }
public double BaseSalary { get; set; }
public override void Input()
{
base.Input();
Console.Write("Nhap bang cap: ");
Degree = Console.ReadLine();
Console.Write("Nhap luong co ban: ");
BaseSalary = double.Parse(Console.ReadLine());
}
public override void Display()
{
base.Display();
Console.WriteLine($"Bang cap: {Degree}, Luong co ban: {BaseSalary:N0}");
}
public virtual double CalcSalary() => BaseSalary;
}
public class OfficeEmployee : Employee
{
public int OvertimeHours { get; set; }
public override void Input()
{
base.Input();
Console.Write("Nhap so gio tang ca: ");
OvertimeHours = int.Parse(Console.ReadLine());
}
public override double CalcSalary()
{
return BaseSalary + OvertimeHours * 50000;
}
public override void Display()
{
base.Display();
Console.WriteLine($"Gio tang ca: {OvertimeHours}, Luong thuc linh: {CalcSalary():N0}");
}
}
public class SalesEmployee : Employee
{
public double SalesAmount { get; set; }
public override void Input()
{
base.Input();
Console.Write("Nhap doanh so ban hang: ");
SalesAmount = double.Parse(Console.ReadLine());
}
public override double CalcSalary()
{
return BaseSalary + SalesAmount * 0.05;
}
public override void Display()
{
base.Display();
Console.WriteLine($"Doanh so: {SalesAmount:N0}, Luong thuc linh: {CalcSalary():N0}");
}
}
Trong Main:
Tạo List<Employee>.
Menu đơn giản:
1. Them nhan vien van phong
2. Them nhan vien kinh doanh
3. Hien thi tat ca
4. Tong quỹ luong
0. Thoat
Thêm nhân viên tương ứng, lưu vào danh sách.
Khi hiển thị, gọi Display().
Tính tổng tiền lương tất cả nhân viên (gọi CalcSalary() từng nhân viên).
Thêm interface IBonus với phương thức double CalcBonus() để sinh viên luyện đa kế thừa qua interface, tương tự ví dụ HinhChuNhat : Shape, ChiPhiSon.
Thêm sắp xếp danh sách nhân viên theo lương giảm dần (cài IComparable<Employee>).
Thêm đọc/ghi danh sách nhân viên ra file text.