C#继承多态
继承与多态
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
将基类的方法定义为虚拟方法,而在派生类中对虚拟方法进行重载.
这种方法的优势在于实现运行时多态性,即程序在运行过程中确定调用哪一个成员方法 .
如果在派生类中使用了override关键字定义了重载方法,那么也允许该类自己的派生类继续重载这个方法,
因此,重载方法默认也是一种虚拟方法.但不能同时使用virtual和override修饰一个方法. 此外,在基类
中定义了虚拟方法.实际上暗示了希望在派生类中继承并重载该方法,因此虚拟方法不能是私有的;而且在基类
和派生类中,对同一个虚拟方法和重载方法的访问限制应该相同.
抽象类之间也可以继承.抽象类要求所有派生类都继承他的抽象方法.包含抽象方法的类必须是抽象类,
因此也需要abstact加以修饰
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CsharpCode
{
/// <summary>
/// 基类:联系人
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public class Contact
{
private string m_name;
private string m_homePhon = "000";
private string m_busiPhone = "000";
public string name
{
get { return m_name; }
set { m_name = value; }
}
public string this[string type]
{
get
{
string _gtype = type.ToUpper();
switch (_gtype)
{
case "住宅电话":
return m_homePhon;
case "办公电话":
return m_busiPhone;
default :
return null;
}
}
set
{
string _stype = type.ToUpper();
switch (_stype)
{
case "住宅电话":
this.m_homePhon = value;
break;
case "办公电话":
this.m_busiPhone = value;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public Contact() { }
public Contact(string name, string homephone, string busiphone)
{
this.m_name = name;
this.m_homePhon = homephone;
this.m_busiPhone = busiphone;
}
public void Output()
{
Console.WriteLine("姓名:{0}",m_name);
Console.WriteLine("住宅电话:{0}", m_homePhon);
Console.WriteLine("办公电话:{0}", m_busiPhone);
}
//采用虚函数定义
public virtual void Display()
{
Console.WriteLine("姓名:{0}", m_name);
Console.WriteLine("住宅电话:{0}", m_homePhon);
Console.WriteLine("办公电话:{0}", m_busiPhone);
}
}
public class Bussiness : Contact
{
private string _Address = "咸宁";
public string Address
{
get { return _Address; }
set { _Address = value; }
}
public Bussiness() { }
/// <summary>
/// 带参数的构造函数用base关键字调用基类的构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="homephone"></param>
/// <param name="busiphone"></param>
/// <param name="address"></param>
public Bussiness(string name, string homephone, string busiphone, string address)
: base(name, homephone, busiphone)
{
this._Address = address;
}
public new void Output()//new关键字隐藏基类相同的方法
{
base.Output();//通过base关键字调用基类隐藏的Ouoput的方法
Console.WriteLine("地址:{0}", _Address);
}
//重载基类的虚方法
public override void Display()
{
base.Output();//通过base关键字访问当前对象的基类对象的方法
Console.WriteLine("地址:{0}", _Address);
}
}
public class Classmate : Contact
{
private string _num = "000";
public string num
{
get { return _num; }
set { _num = value; }
}
public Classmate() { }
/// <summary>
/// 带参数的构造函数用base关键字调用基类的构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="homephone"></param>
/// <param name="busiphone"></param>
/// <param name="address"></param>
public Classmate(string name, string homephone, string busiphone, string num)
: base(name, homephone, busiphone)
{
this._num = num;
}
//重载基类的虚方法
public override void Display()
{
base.Output();//通过base关键字访问当前对象的基类对象的方法
Console.WriteLine("学号:{0}", _num);
}
}
class Program
{
static void Main(string[] args)
{
Contact c1 = new Contact();
c1.name = "咸宁";
c1["住宅电话"] = "123456789";
c1["办公电话"] = "987654321";
c1.Output();
Console.WriteLine("--------------------");
Bussiness b1 = new Bussiness();
b1.name = "张三";
b1["办公电话"] = "222222";
b1.Address = "咸宁温泉";
b1.Output();
Console.WriteLine("--------------------");
Contact c2 = new Contact("111", "13596357854", "13485697699");
c2.Output();
Console.WriteLine("通过构造函数来实现");
Bussiness b2 = new Bussiness("222", "13688888888", "137899999999","咸安温泉大道");
b2.Output();
Console.WriteLine("通过虚函数的方法来实现");
//将基类的方法定义为虚拟方法,而在派生类中对虚拟方法进行重载.
//这种方法的优势在于实现运行时多态性,即程序在运行过程中确定调用哪一个成员方法
Bussiness b3 = new Bussiness("333","13896326548","13963251478","咸宁市温泉区咸宁大道中心校区");
Classmate classmate = new Classmate("444", "13855555558", "13977777777", "010004442");
Contact c3 = b3;
//把派生类类对象赋给基类对象,通过基类的对象(实际指向派生类)去调用派生类的方法
c3.Display();
c3 = classmate;
c3.Display();
}
}
}