Nhưng khi xây dựng một giao diện thì ta phải bắt đầu bằng từ khóa interface thay vì từ khóa class đứng trước tên lớp.. Trong Interface, tất cả các phương thức method và thuộc tính proper
Trang 1Lập trình hướng đối tượng
Giao diện (Interface)
1 Giao diện (interface) là một dạng của lớp trừu tượng Bình thường, khi xây dựng một lớp, ta dùng từ khóa class Nhưng khi xây dựng một giao diện thì ta phải bắt đầu bằng từ khóa interface (thay vì từ khóa class) đứng trước tên lớp.
2 Trong Interface, tất cả các phương thức (method) và thuộc tính (property) không được triển khai code mà chỉ khai báo theo dạng prototype.
3 Trong Interface, mặc dù trước mỗi phương thức và property không có từ khóa abstract và public nhưng mặc nhiên các phương thức và property của nó là dạng trừu tượng (abstract) và có phạm vi truy cập là public
4 Một lớp abstract thì có thể chứa phương thức (hay property) dạng abstarct hoặc dạng bình thường Nhưng các Interface thì chỉ chứa các phương thức (hoặc
property) ngầm định là abstract mà thôi.
5 Ta không thể khởi tạo các Interface bằng từ khóa new (giống trường hợp lớp abstract) Interface giống như là một khuôn mẫu để cho các lớp con thừa kế nó Khi thừa kế Interface thì các lớp con của nó bắt buộc phải viết code cho tất cả các phương thức và property từ Interface.
6 Trong C#, một lớp con chỉ được thừa kế từ một lớp cha Nhưng một lớp con có thể thừa kế từ nhiều Interface Một Interface cũng có thể thừa kế từ các Interface khác.
7 Trong Interface không được khai báo hay triển khai code cho các constructor.
8 Tại lớp con, khi triển khai code cho các phương thức thừa kế từ Interface, ta
không cần thiết phải có từ khóa override trước tên phương thức.
Trang 2namespace Interface_NV
{
public enum Certificate
{
TienSi = 0,
ThacSi,
CuNhan,
KySu,
CaoDang,
TrungCap,
KyThuatVien
}
public enum Position
{
GiamDoc = 0,
PhoGiamDoc,
TruongPhong,
PhoPhong,
}
public interface I_NhanVien
{
void Nhap();
float TinhLuong();
//khong the khai bao bien hoac viet constructor o day String HoTen { get; set; }
}
class NhaKhoaHoc : I_NhanVien
{
private String hoten;
private int namsinh;
private Certificate bangcap;
private Position chucvu;
private int sobaibao;
private int songaycong;
private float bacluong;
public NhaKhoaHoc()
{
hoten = "";
namsinh = 1900;
bangcap = Certificate.KyThuatVien;
chucvu = Position.PhoPhong;
sobaibao = 0;
Trang 3songaycong = 0;
bacluong = 0;
}
public NhaKhoaHoc(String ht, int ns, Certificate bc, Position cv, int sbb, int snc, int bl)
{
hoten = ht;
namsinh = ns;
bangcap = bc;
chucvu = cv;
sobaibao = sbb;
songaycong = snc;
bacluong = bl;
}
public override string ToString()
{
return "[" + hoten + "," + namsinh + "," + bangcap.ToString() +"," +
chucvu.ToString() + "," + sobaibao + "," + songaycong + "," + bacluong + "," +
TinhLuong() + " trieu dong" + "]";
}
public void Nhap()
{
Console.Write("Nhap ho ten:");
hoten = Console.ReadLine();
Console.Write("Nhap nam sinh:");
namsinh = int.Parse(Console.ReadLine());
Console.WriteLine("Nhap mot con so cua bang cap nhu sau:");
foreach (Certificate value in Enum.GetValues(typeof(Certificate)))
Console.WriteLine("{0}.{1}", (int)value, value.ToString());
bangcap = (Certificate)int.Parse(Console.ReadLine());
Console.WriteLine("Nhap mot con so cua chuc vu nhu sau:");
foreach (Position value in Enum.GetValues(typeof(Position)))
Console.WriteLine("{0}.{1}", (int)value, value.ToString());
chucvu = (Position)int.Parse(Console.ReadLine());
Console.Write("Nhap so bai bao:");
sobaibao = int.Parse(Console.ReadLine());
Console.Write("Nhap so ngay cong:");
songaycong = int.Parse(Console.ReadLine());
Console.Write("Nhap bac luong: ");
bacluong = float.Parse(Console.ReadLine());
}
public float TinhLuong()
Trang 4{
return ((2 * bacluong) / 26) * songaycong; //2 trieu VND
}
public string HoTen
{
get
{
return hoten;
}
set
{
hoten = value;
}
}
}
class NhaQuanLy : I_NhanVien
{
private String hoten;
private int namsinh;
private Certificate bangcap;
private Position chucvu;
private int songaycong;
private float bacluong;
public NhaQuanLy()
{
hoten = "";
namsinh = 1900;
bangcap = Certificate.KyThuatVien;
chucvu = Position.PhoPhong;
songaycong = 0;
bacluong = 0;
}
public NhaQuanLy(String ht, int ns, Certificate bc, Position cv, int snc, int bl) {
hoten = ht;
namsinh = ns;
bangcap = bc;
chucvu = cv;
songaycong = snc;
bacluong = bl;
}
Trang 5public override string ToString()
{
return "[" + hoten + "," + namsinh + "," + bangcap.ToString() + "," +
chucvu.ToString() + "," + songaycong + "," + bacluong + "," + TinhLuong() + " trieu dong" + "]";
}
public void Nhap()
{
Console.Write("Nhap ho ten:");
hoten = Console.ReadLine();
Console.Write("Nhap nam sinh:");
namsinh = int.Parse(Console.ReadLine());
Console.WriteLine("Nhap mot con so cua bang cap nhu sau:");
foreach (Certificate value in Enum.GetValues(typeof(Certificate)))
Console.WriteLine("{0}.{1}", (int)value, value.ToString());
bangcap = (Certificate)int.Parse(Console.ReadLine());
Console.WriteLine("Nhap mot con so cua chuc vu nhu sau:");
foreach (Position value in Enum.GetValues(typeof(Position)))
Console.WriteLine("{0}.{1}", (int)value, value.ToString());
chucvu = (Position)int.Parse(Console.ReadLine());
Console.Write("Nhap so ngay cong:");
songaycong = int.Parse(Console.ReadLine());
Console.Write("Nhap bac luong: ");
bacluong = float.Parse(Console.ReadLine());
}
public float TinhLuong()
{
return ((2 * bacluong) / 26) * songaycong; //2 trieu VND
}
public string HoTen
{
get
{
return hoten;
}
set
{
hoten = value;
}
}
}
Trang 6class NhanVienPTN : I_NhanVien
{
private String hoten;
private int namsinh;
private Certificate bangcap;
private float luongthang;
public NhanVienPTN()
{
hoten = "";
namsinh = 1900;
bangcap = Certificate.KyThuatVien;
luongthang = 0;
}
public NhanVienPTN(String ht, int ns, Certificate bc, float lt)
{
hoten = ht;
namsinh = ns;
bangcap = bc;
luongthang = lt;
}
public override string ToString()
{
return "[" + hoten + "," + namsinh + "," + bangcap.ToString() + "," + TinhLuong() + " trieu dong" + "]";
}
public void Nhap()
{
Console.Write("Nhap ho ten:");
hoten = Console.ReadLine();
Console.Write("Nhap nam sinh:");
namsinh = int.Parse(Console.ReadLine());
Console.WriteLine("Nhap mot con so cua bang cap nhu sau:");
foreach (Certificate value in Enum.GetValues(typeof(Certificate))) Console.WriteLine("{0}.{1}", (int)value, value.ToString());
bangcap = (Certificate)int.Parse(Console.ReadLine());
Console.Write("Nhap luong thang: ");
luongthang = float.Parse(Console.ReadLine());
}
Trang 7public float TinhLuong()
{
return luongthang;
}
public string HoTen
{
get
{ return hoten;}
set
{ hoten = value;}
}
}
class Program
{
static void Main(string[] args)
{
I_NhanVien nv = new NhaKhoaHoc();
//nv = new I_NhanVien(); //bao loi
nv.Nhap();
Console.WriteLine(nv);
Console.ReadKey();
Console.WriteLine("Doi tuong NHA KHOA HOC:");
NhaKhoaHoc nkh = new NhaKhoaHoc();
Console.WriteLine(nkh);
nkh.Nhap();
Console.WriteLine(nkh);
Console.WriteLine();
Console.WriteLine("Doi tuong NHA QUAN LY:");
NhaQuanLy nql = new NhaQuanLy();
Console.WriteLine(nql);
nql.Nhap();
Console.WriteLine(nql);
Console.WriteLine();
Console.WriteLine("Doi tuong NHAN VIEN PHONG THI NGHIEM:"); NhanVienPTN nvptn = new NhanVienPTN();
Console.WriteLine(nvptn);
nvptn.Nhap();
Console.WriteLine(nvptn);
Console.WriteLine();
Console.ReadKey();
}
}
}
Cách 2:
public interface I_NhanVien
Trang 8{
void Nhap();
float TinhLuong();
//khong the khai bao bien hoac viet constructor o day
String HoTen { get; set; }
}
class NhanVien: I_NhanVien
{ protected String hoten;
protected int namsinh;
protected Certificate bangcap;
public NhanVien()
{ hoten = "Chua co ten";
namsinh = 1900;
bangcap = Certificate.KyThuatVien;
}
public NhanVien(String hoten, int namsinh, Certificate bangcap) { this.hoten = hoten;
this.namsinh = namsinh;
this.bangcap = bangcap;
}
public override string ToString()
{ return hoten + "," + namsinh + "," + bangcap.ToString();
}
public void Nhap()
{ Console.Write("Nhap ho ten:");
hoten = Console.ReadLine();
Console.Write("Nhap nam sinh:");
namsinh = int.Parse(Console.ReadLine());
Console.WriteLine("Nhap mot con so cua bang cap nhu sau:"); foreach (Certificate value in Enum.GetValues(typeof(Certificate))) Console.WriteLine("{0}.{1}", (int)value, value.ToString()); bangcap = (Certificate)int.Parse(Console.ReadLine());
}
public float TinhLuong(){ return 0; }
public string HoTen
{
get
{
return hoten;
}
set
{
hoten = value;
}
}
}