Do it yourself
Class Person
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPT_Day04
{
public abstract class Person
{
public string Name { get; set; }
public string PhoneNum { get; set; }
public string Address { get; set; }
protected Person(string pName, string pPhoneNum, string pAddress)
{
Name = pName;
PhoneNum = pPhoneNum;
Address = pAddress;
}
}
}
Class Student
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPT_Day04
{
class Student : Person
{
public string Program { get; set; }
public Student(string pProgram, string pName, string pPhoneNum, string pAddress) : base(pName, pPhoneNum, pAddress)
{
Program = pProgram;
}
public override string ToString()
{
return "+Student name: " + Name + "\n+Program: " + Program + "\n+PhoneNum: " + PhoneNum + "\n+Address: " + Address;
}
}
}
Class Employee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPT_Day04
{
public abstract class Employee : Person
{
public string Department { get; set; }
public double Salary { get; set; }
public DateTime DateHired { get; set; }
public Employee(string pName, string pPhoneNum, string pAddress, string pDepartment, double pSalary, DateTime pDateHired) : base(pName, pPhoneNum, pAddress)
{
Department = pDepartment;
Salary = pSalary;
DateHired = pDateHired;
}
public override string ToString()
{
return "\n+Employee name: " + Name + "\n+Phone: " + PhoneNum + "\n+Address: " + Address + "\n+Department: " + Department + "\n+Salary: " + Salary + "\n+DateHired: " + DateHired;
}
public abstract double CaculateBonus();
public abstract double CalculateVacation();
}
}
Class Faculty
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPT_Day04
{
class Faculty : Employee
{
public double OfficeHours { get; set; }
public string Rank { get; set; }
public Faculty(string pName, string pPhoneNum, string pAddress, string pDepartment, double pSalary, DateTime pDateHired, double pOfficeHours,string pRank) : base(pName, pPhoneNum, pAddress, pDepartment, pSalary, pDateHired)
{
OfficeHours = pOfficeHours;
Rank = pRank;
}
public override double CaculateBonus()
{
return 1000 + 0.05 * Salary;
}
public override double CalculateVacation()
{
DateTime today = DateTime.Today;
int YearsOfExp = today.Year - DateHired.Year;
if (YearsOfExp >= 3 && Rank == "Senior Lecturer")
{
return 6;
}
else if(YearsOfExp >= 3)
{
return 5;
}
else
{
return 4;
}
}
public override string ToString()
{
return "\n+Faculty name: " + Name + "\n+Phone: " + PhoneNum + "\n+Address: "
+ Address + "\n+Department: " + Department + "\n+Salary: " + Salary +
"\n+DateHired: " + DateHired + "\n+Offices Hours: " + OfficeHours
+ "\n+Rank: " + Rank;
}
}
}
Class Staff
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPT_Day04
{
class Staff : Employee
{
public string Title { get; set; }
public Staff(string pName, string pPhoneNum, string pAddress, string pDepartment, double pSalary, DateTime pDateHired, string pTitle) : base(pName, pPhoneNum, pAddress, pDepartment, pSalary, pDateHired)
{
Title = pTitle;
}
public override double CaculateBonus()
{
return 0.06 * Salary;
}
public override double CalculateVacation()
{
DateTime today = DateTime.Today;
int YearsOfExp = today.Year - DateHired.Year;
if (YearsOfExp >= 5)
{
return 4;
}
else
{
return 3;
}
}
public override string ToString()
{
return "\n+Staff name: " + Name + "\n+Phone: " + PhoneNum + "\n+Address: "
+ Address + "\n+Department: " + Department + "\n+Salary: " + Salary +
"\n+DateHired: " + DateHired + "\n+Title: " + Title;
}
}
}
Class Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FPT_Day04
{
class Program
{
static void Main(string[] args)
{
Student sv01 = new Student("FPT Aptech", "Jimmy Hoang", "123456789", "Vincom");
Console.WriteLine(sv01.ToString());
Faculty fal01 = new Faculty("Tony Stark", "123456", "California", "The Avenger Inc", 1000000, new DateTime(2005, 03, 03), 300, "Senior Lecturer");
Console.WriteLine(fal01.ToString());
Console.WriteLine("=> Mr." + fal01.Name + " salary BONUS: " + fal01.CaculateBonus());
Console.WriteLine("=> Mr." + fal01.Name + " vacation DAYS: " + fal01.CalculateVacation());
Faculty fal02 = new Faculty("Thor", "654321", "Asgard", "The Avenger Inc", 5000000, new DateTime(2014, 08, 22), 50, "Newbie");
Console.WriteLine(fal02.ToString());
Console.WriteLine("=> Mr." + fal02.Name + " salary BONUS: " + fal02.CaculateBonus());
Console.WriteLine("=> Mr." + fal02.Name + " vacation DAYS: " + fal02.CalculateVacation());
Staff sta01 = new Staff("PlayBoy", "465836", "Vincom", "Democracy Inc", 250000, new DateTime(2010, 05, 11), "Big Penis");
Console.WriteLine(sta01.ToString());
Console.WriteLine("=> Mr." + sta01.Name + " salary BONUS: " + sta01.CaculateBonus());
Console.WriteLine("=> Mr." + sta01.Name + " vacation WEEKS: " + sta01.CalculateVacation());
Staff sta02 = new Staff("PlayGirl", "696969", "Japan", "Communism Inc", 460000, new DateTime(2013, 01, 25), "Big Tities");
Console.WriteLine(sta02.ToString());
Console.WriteLine("=> Ms." + sta02.Name + " salary BONUS: " + sta02.CaculateBonus());
Console.WriteLine("=> Ms." + sta02.Name + " vacation WEEKS: " + sta02.CalculateVacation());
Console.ReadLine();
}
}
}
Result:
Summary:
- Abstraction ( Tính trừu tượng )
- Khi ta muốn tạo ra 1 class mà method trong đó chưa biết viết "body" của nó thế nào, thì ta dùng abstract
- Example:
abstract class ShapesClass { abstract public int Area(); }
- 1 class mà derive từ abstract class thì phải implement tất cả các method từ abstract class đó ( trừ khi method đó dùng "virtual" )
- Example:
class Square : ShapesClass { int side = 0; public Square(int n) { side = n; } // Area method is required to avoid // a compile-time error. public override int Area() { return side * side; }
}
- Abstract class không instantiate (khởi tạo) được.
- Interface không instantiate được.
- 1 subclass có thể inherit nhiều interfaces.
- Một interface không bao gồm hằng số, biến, destructer, các thành phần static.
- Giống:
- Cả hai đều không thể khởi tạo đối tượng.
- Cả hai đều khai báo các phương thức nhưng không thực hiện chúng.
- Cả hai đều bao gồm các phương thức abstract.
- Cả hai đều được thực thi từ các class kế thừa.
- Cả hai đều có thể kế thừa từ nhiều interface.
- Khác:
4. Indexer
- Indexer là các thành phần dữ liệu cho phép bạn truy suất dữ liệu bên trong các đối tượng một cách tương tự như cách truy suất mảng.
No comments:
Post a Comment