---------------------- <ahref="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a>、<ahref="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
继承:
根据已有的类作为基础,可派生出新的类,这时可将原有的类称为基类或父类,派生出来的新类为派生类或子类,派生类可继承基类的成员,同时也可以定义自己特有的新成员。
1. 派生类的定义格式:
访问修饰符 class 派生类名称:基类名称
{ 数据成员的定义
方法成员的定义
}
例如:若有以下一个客车类的定义:
classPasstrain //定义一个passtrain类
{
intweight; //车的重量
private intpassengers; //标准容纳乘客数
public intwheels; //车轮数量
public stringplate; //车牌号
publicvoid Showinfo() //显示客车的信息
{
Console.WriteLine("weight="+ weight );
Console.WriteLine("passengers="+ passengers);
Console.WriteLine("wheels=" + wheels);
Console.WriteLine("plate="+ plate);
}
}
可以定义一个由客车类派生出来的出租车类,定义如下:
classTaxis : Passtrain
{
privatefloat price;//记录每公里价格的字段
publicfloat Price//可读写的计费标准属性
{
get{ returnprice; }
set{ price = value;}
}
publicfloat Money(floatdis)//根据里程数计算付费金额的方法
{
returndis * price;
}
}
2. 派生类对基类的继承
这时,由客车类派生出来的出租车类可继承客车类的weight、passengers、wheels、plate等数据成员,以及Showinfo()方法成员。
同时还对基类进行了扩展,定义了自己独有的成员:price字段,Price属性以及Money方法。例如:
classProgram
{
staticvoid Main(string[]args)
{
Taxist = newTaxis();
Console.WriteLine(t.plate);//访问继承自基类的成员字段
t.Price = 2.5f;//访问自定义的成员属性
Console.WriteLine(t.Money(4.3f));//访问自定义的成员方法
t.Showinfo();//访问继承自基类的成员方法
}
}
3. 继承与保护
由于派生类有自己特有的成员字段,在通过继承而来的Showinfo方法来输出其基本信息时,信息并不完全,这就需要定义自己的输出其基本信息的方法,因为其功能与Showinfo方法是一样的,这是输出的内容更多一些,因此这个方法的名称应与Showinfo方法相同。
publicvoid Showinfo() //显示客车的信息
{
Console.WriteLine("weight="+ weight);
Console.WriteLine("passengers="+ passengers);
Console.WriteLine("wheels="+ wheels);
Console.WriteLine("plate="+ plate);
Console.WriteLine("price="+ price);
}
加入这段代码后,运行时会产生一个警告和两个错误提示:
警告 1 “客车类的派生类_出租车_.Taxis.Showinfo()”隐藏了继承的成员“客车类的派生类_出租车_.Passtrain.Showinfo()”。如果是有意隐藏,请使用关键字 new。
这是因为派生类已继承发基类的Showinfo方法,又定义了自已的Showinfo方法,这两个方法同时存在并且重名,系统虽然能知道应优先使用自己定义的Showinfo方法,但最好还是明确说明要用新定义的Showinfo方法来隐藏原来的Showinfo方法。加入new关键字后,可消除这个警告。
public new void Showinfo() //显示客车的信息
另两个错误提示出错的原因相同的:
错误 1 “客车类的派生类_出租车_.Passtrain.weight”不可访问,因为它受保护级别限制。
这是因为派生类虽已继承了这个私有的成员weight 但它因为是私有的,不能在出租车这个派生类中访问,只能通过原来的Showinfo方法访问。
若要让派生类也能访问有两个办法:
1. 将客车类中定义的访问属性改为public,这样错误可以消除,但在程序的其它地方,也可以访问这个属性,未起到保护作用。
2. 将客车类中定义的访问属性改为protected,这样错误也可以消除,且在程序的其它地方,不可以访问这个属性,起到保护作用。
4. 继承的传递性
需要说明的是,派生类对基类的继承有传递性。举例说明,例子略。
5. 继承与构造函数
在客车类的定义中加入一个构造函数的定义:
publicPasstrain()
{
weight = 30;
passengers = 40;
wheels = 6;
plate = "000000";
}
将主程序改为:
staticvoid Main(string[]args)
{
Taxist = newTaxis();
t.Showinfo();//访问继承自基类的成员方法
Console.WriteLine("price="+t.Price);
}
运行程序时,可以看到基类和派生类的构造函数都可以被自动运行。这是因为:
系统是在构造一个派生类的对象时,先自动运行基类的构造函数,再运行派生类的构造函数。
在客车类的定义中加入一个构造函数的定义:
publicPasstrain(intwe, intpa, intwh, stringpl)
{
weight = we;
passengers = pa;
wheels = wh;
plate = pl;
}
若将程序改为:
Taxis t = newTaxis(1,2,3,"SDFG");
程序运行时报错:
错误 1 “客车类的派生类_出租车_.Taxis”不包含采用“4”参数的构造函数
这是因为派生类未定义带有没有个参数的构造函数,不能将这4个参数传递给它的基类的“4”参数的构造函数。解决的方法为:
为派生类定义一个“4”参数的构造函数,并将参数传递给基类的“4”参数的构造函数。
publicTaxis(int we, intpa, int wh, stringpl) : base(we,pa, wh, pl)
{ }
基类的构造函数与派生类的构造函数的执行顺序:
classPersonclass
{
protectedPersonclass(stringT, stringM)
{
Console.WriteLine("基类中构造函数的内容:");
Console.WriteLine("基类中的构造函数被继承!");
Console.WriteLine("传递的参数:{0}", T);
Console.WriteLine("传递的参数:{0}", M);
Console.WriteLine();
}
}
classStudentclass : Personclass
{
publicStudentclass(stringN, stringI)
: base(N, I)
{
Console.WriteLine("派生类中构造函数的内容:");
Console.WriteLine("参数设置的姓名是:{0}", N);
Console.WriteLine("参数设置的ID是:{0}", I);
}
}
classProgram
{
staticvoid Main(string[]args)
{
Studentclasst1 = newStudentclass("xusen","20021308008");
}
}
构造函数的参数传递顺序:
classFirstclass
{
protectedFirstclass(stringA, stringB)
{
Console.WriteLine("第一个基类中内容:");
Console.WriteLine("第一个基类中的内容被继承!");
Console.WriteLine("传递的参数:{0}", A);
Console.WriteLine("传递的参数:{0}", B);
Console.WriteLine();
}
}
classPersonclass : Firstclass
{
protectedstring a;
protectedPersonclass(stringT, stringM)
: base(T, M)
{
Console.WriteLine("基类中构造函数的内容:");
Console.WriteLine("基类中的构造函数被继承!");
Console.WriteLine("传递的参数:{0}", T);
Console.WriteLine("传递的参数:{0}", M);
Console.WriteLine();
}
}
classStudentclass : Personclass
{
publicStudentclass(stringN, stringI)
: base(N, I)
{
Console.WriteLine("派生类中构造函数的内容:");
Console.WriteLine("参数设置的姓名是:{0}", N);
Console.WriteLine("参数设置的ID是:{0}", I);
}
}
classProgram
{
staticvoid Main(string[]args)
{
Studentclasst1 = newStudentclass("xusen","20021308008");
}
}