【转】抽象类那点事(一步步案例分析,为什么需要它)
【http://www.cnblogs.com/IAmBetter/archive/2011/12/21/2296196.html】
说一句题外话,本人学C# 不到一个月时间,今天同事说了2个 区别 抽象类与区别,我感觉请教前辈 超过自己的看10个小时的理解。
抽象类
abstract
抽象方法的类要声明为抽象类
构造函数和静态方法不能声明为抽象
多态性是一个虚方法,可以重写这个方法,实现同一个方法不同的表现形式
对于抽象方法的重写,是说的对于这个方法的派生类的实现。
对于抽象类和接口的方法都需要实现。
抽象方法是无法写方法体的
抽象类可以写 实现的方法
接口不可以写 实现的方法
案例1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public abstract class Employee//雇员抽象类
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Ssn { get; private set; }
public Employee(string first, string last, string ssn)
{
FirstName = first;
LastName = last;
Ssn = ssn;
}
public abstract decimal Earning();
public virtual int Time(int times)//抽象类可以写虚方法
{
return times;
}
public override string ToString()
{
return string.Format("FirstName:{0} LastName:{1} SSN:{2}", FirstName, LastName, Ssn);
}
}
public class SalariedEmployee : Employee//薪水雇员类
{
private decimal weeksalay;
public SalariedEmployee(string first, string last, string ssn, decimal salay)
: base(first, last, ssn)//调用抽象类的构造函数
{
Weeksalay = salay;
}
public decimal Weeksalay
{
get { return weeksalay; }
set
{
weeksalay = (value > 0) ? value : 0;
}
}
public override decimal Earning()
{
Weeksalay = Weeksalay* 7;
return Weeksalay;
}
public override string ToString()//如果不重写ToString方法那么继承的是抽象类的方法
{
return string.Format("{0} Weeksalay:{1}",base.ToString(), Weeksalay);
}
}
class HourEmployee : Employee//小时工类
{
private decimal wage;
private int hour;
public HourEmployee(string first, string last, string ssn, decimal hourwage, int hourtime)
: base(first, last, ssn)
{
Hour = hourtime;//注意这里,2个属性的前后顺序会决定了结果的
Wage = hourwage;
}
public decimal Wage
{
get { return wage; }
set
{
wage = (value > 0) ? value : 0;
}
}
public int Hour
{
get { return hour; }
set
{
hour = (value > 0) ? value : 0;
}
}
public override decimal Earning()
{
if (Hour <= 40)
{
Wage = Wage* Hour;
}
else
{
Wage = Wage* 40 + (Hour - 40) * Wage* 1.5m;
}
return Wage;
}
public override string ToString()
{
return string.Format("{0} Hourwage:{1} Hourtimes:{2},Wage:{3}", base.ToString(), Wage, Hour, Wage);//这里继承抽象类的ToString方法
}
}
class BaseHe_Employee : HourEmployee//间接继承抽象类
{
private decimal rate;
private decimal salay;
public BaseHe_Employee(string first, string last, string ssn, decimal hourwage, int hourtime, decimal rate, decimal salay)
: base(first, last, ssn, hourwage, hourtime)
{
Rate = rate;
Salay = salay;
}
public decimal Rate
{
get { return rate; }
set { rate = (value > 0) ? value : 0; }
}
public decimal Salay
{
get { return salay; }
set { salay = (value > 0) ? value : 0; }
}
public override decimal Earning()
{
return Salay+ base.Earning();
}
public override string ToString()
{
return string.Format("{0} Rate:{1},Salay:{2}", base.ToString(), Rate, Salay);//这里继承抽象类的ToString方法
}
}
class Test
{
static void Main()
{
//SalariedEmployee oneSE = new SalariedEmployee("Mr.w", "Mery An", "10086", 6500.00m);
//Console.WriteLine(oneSE.ToString());
BaseHe_Employee oneBHE = new BaseHe_Employee("Mr.w", "Mery An", "10086", 6.50m, 24, 15.5m, 16.5m);
Console.WriteLine(oneBHE.ToString());
Console.WriteLine(oneBHE.Earning());
//HourEmployee oneHE = new HourEmployee("Mr.w", "Mery An", "10086", 6.50m, 24);
//Console.WriteLine(oneHE.ToString());
//Console.WriteLine(oneHE.Earning());
}
}
}