Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 261

Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 261
_higrid.net-嗨网
当前位置:嗨网首页>书籍在线阅读

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 266

Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 266

Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 266

Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 266

10.4.2 实现抽象方法

C#中通过方法重写来实现抽象方法。当一个抽象基类派生一个派生类时,派生类将继承基类的所有特征,重新实现所有的抽象方法。在派生类中实现基类的抽象方法,是使用override关键字来重写基类方法。

例如,为10.4.1小节中声明的抽象类Employee创建了一个名为 Manager 的派生类,实现 Employee 类中的抽象方法 Display。

01  public class Manager: Employee            //创建Employee的派生类Manager类
02  {  
03          private string name; 
04          public Manager(string name)       //构造函数实现对name初始化
05          {        this.name = name;  } 
06          public override void Display()    //重写实现Employee中的抽象方法Display
07          {  
08                  Console.WriteLine("Name : " + name);  //输出name
09          }
10  }

可以创建Manager类的对象,调用Display方法显示Manager的name。例如,

01  Manager obj=new Manager("Johnson");        // 创建Manager类的对象
02  obj.Diaplay();                             //将显示姓名为"Johnson"


Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 276

Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 276

Warning: Undefined array key 0 in /www/wwwroot/w/show/success.php on line 276

Warning: Trying to access array offset on value of type null in /www/wwwroot/w/show/success.php on line 276