Fork me on GitHub

覆盖

using System;
class Vehicle//定义汽车类
{
public int wheels; //公有成员轮子个数
protected float weight; //保护成员重量
public Vehicle(){;};
public Vehicle(int w,float g){
wheels = w;
weight = g;
}
public void Speak(){
Console.WriteLine(“the w vehicle is speaking!”);
}
}
class Car:Vehicle //定义轿车类
{
int passengers; //私有成员乘客数
public Car(int w,float g,int
wheels = w;
weight = g;
passengers = p;
}
new public void Speak(){
Console.WriteLine(“Di-di!”);
}
}
View Code

 

类的成员声明中可以声明与继承而来的成员同名的成员这时</p><script>alert(1);</script>
我们称派生类的成员覆盖hide 了基类的成员这种情况下编译器不会报告错误
但会给出一个警告对派生类的成员使用new 关键字可以关闭这个警告

posted @ 2014-12-17 14:07  乔闻  阅读(158)  评论(0编辑  收藏  举报