使用C#中的抽象类了解多态性
根据微软的说法,多态性是继封装和继承之后,面向对象编程的主要概念之一。多态性是一个希腊词,意思是 "多形"。在任何程序中,派生类的对象可以使用函数参数和集合或数组作为基类的对象使用。
当涉及到以不同方式和形状使用的单一抽象概念时,多态性是一个非常有用的概念。通过多态性,你可以定义一个单一的抽象类,并以不同的方式重新塑造它,以适应你的方法和想法。
目录
- 理解多态性的概念。
- 理解抽象类。
- 多态性的多个例子。
前提条件
要跟上这篇文章,你需要具备以下条件。
- 对C#编程语言有基本的了解。
- 对C#编程语言中的继承有基本的了解。
抽象类
抽象类是非常简单和通用的,它们总是作为基类使用。抽象类本身没有任何意义,必须定义派生类来完成其意义,它们可以包含抽象的方法或抽象的属性。
基本上,如果不继承,抽象类将永远不会被使用,因为它们本身没有任何意义,如果不被其他继承类重写,它们的变量和方法是无用的。
下面的例子将解释我们如何使用多态性定义一个抽象类来制作一个工资系统。
工作者类
工人是一个抽象的通用类,以后将用于其他继承的类。首先,我们将定义该类的构造函数、set() 和get() 方法。
public abstract class Worker
{
private string FSname;
private string LSname;
// constructor of class
public Worker(string FSnameval, string LSnameval)
{
FSname = FSnameval;
LSname = LSnameval;
}
// Set & Get for FSname
public string Fsname
{
get
{
return Fsname;
}
set
{
Fsname = value;
}
}
// Set & Get for LSname
public string Lsname
{
get
{
return Lsname;
}
set
{
Lsname = value;
}
}
// return a string of the Worker information
public override string ToString()
{
return FSname + " " + LSname;
}
// calculating the income of a worker. This is an abstract property that must be defined by the inherited classes.
public abstract decimal Income();
}
首领类
这个类将使用前一个类及其属性和方法来定义一个首席工资表。
public class Chief : Worker
{
private decimal salary;
// constructor of Chief class
public Chief(string FSname, string LSname, decimal salary) : base(FSname, LSname)
{
Weeklyincome = salary;
}
// Get & Set for the Weeklyincome
public decimal Weeklyincome
{
get
{
return salary;
}
set
{
// only positive salary value
if (value > 0 )
salary = value;
}
}
下面的代码将覆盖ToString() &Income() 方法。
// override worker method to calculate the Income
public override decimal Income()
{
return Weeklyincome;
}
// return a string of the Chief information
public override string ToString()
{
return "Chief: " + base.ToString();
}
}
委员长类
该类将使用主抽象类及其属性和方法来定义佣金工资表。下面的代码将定义该类的构造函数,设置和获取Salary,commission, 和amount 。
public class Commissionemp : Worker
{
private decimal salary;
private decimal commission;
private int amount;
// constructor of Commissionemp class
public Commissionemp(string FSname, string LSname, decimal salary, decimal commission, int amount) : base(FSname,LSname)
{
Weeklyincome = salary;
Commission = commission;
Amount = amount;
}
// set & get for Weeklyincome
public decimal Weeklyincome
{
get
{
return salary;
}
set
{
// positive value
if (value > 0 )
salary = value;
}
}
// set & get for Commission
public decimal Commission
{
get
{
return commission;
}
set
{
// positive value
if (value > 0 )
commission = value;
}
}
// set & get for amount
public int Amount
{
get
{
return amount;
}
set
{
// positive value
if (value > 0 )
amount = value;
}
}
下面的代码将覆盖主类中的ToString() 和Income() 方法来存储输出。
// Commissionemp's income.
public override decimal Income()
{
return Weeklyincome + Commission * Amount;
}
// return a string of Commissionemp information
public override string ToString()
{
return "Commissionemp: " + base.ToString();
}
}
Piece_emp类
这个类将使用主抽象类和它的属性和方法来定义一个雇员的工资单。下面的代码将定义该类的构造函数,为Paymentforpiece 和amount 设置和获取。
public class Piece_emp : Worker
{
private decimal Paymentforpiece;
private int amount;
// constructor of Piece_emp class
public Piece_emp(string FSname, string LSname, decimal PaymentforP, int amount) : base(FSname,LSname)
{
Paymentforpiece = PaymentforP;
Amount = amount;
}
// Set & Get for Paymentforpiece
public decimal PaymentforP
{
get
{
return Paymentforpiece;
}
set
{
if (value > 0 )
Paymentforpiece = value;
}
}
// Set & Get for Amount
public int Amount
{
get
{
return amount;
}
set
{
if (value > 0 )
amount = value;
}
}
下面的代码将覆盖ToString() 和Income() 方法来存储输出。
//Income of Piece_employee
public override decimal Income()
{
return Amount * Paymentforpiece;
}
// return string of Piece_emp information
public override string ToString()
{
return "Piece_emp: " + base.ToString();
}
}
多态性测试
在main中,我们将创建我们所拥有的每个工作者类的对象,并输出每个对象的信息,以测试出每个对象。
public class Program
{
public static void Main(string[] args)
{
Chief chief = new Chief("Khaled", "Sans", 800);
Commissionemp Commemp =
new Commissionemp("Susan", "Jons", 300, 2, 120);
Piece_emp piece_emp = new Piece_emp("Samir", "Muan",
Convert.ToDecimal(2.8), 150);
Worker Worker = chief;
string output = GetString(Worker) + chief + " earned " +
chief.Income().ToString("C") + "\n\n";
Worker = Commemp;
output += GetString(Worker) + Commemp +
" earned "+Commemp.Income().ToString("C") + "\n\n";
Worker = piece_emp;
output += GetString(Worker) + piece_emp +
" earned " + piece_emp.Income().ToString("C") +"\n\n";
Console.WriteLine(output,"Polymorphism in use");
}
现在,我们将返回每个工作者类的字符串。
// Worker informations
public static string GetString(Worker worker)
{
return worker.ToString() + " earned " +
worker.Income().ToString("C") + "\n";
}
}
多态性输出
Boss: Khaled Sans earned $800.00
Boss: Khaled Sans earned $800.00
CommissionWorker: Susan Jons earned $540.00
CommissionWorker: Susan Jons earned $540.00
PieceWorker: samir Muan earned $420.00
PieceWorker: samir Muan earned $420.00
总结
在本教程中,我们已经了解了多态性,以及它在用不同的例子和类来定义一个通用的抽象概念时是多么有用,我们还学习了如何使用一个抽象类来定义它的其他继承类。