override(C# 参考)

要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现,必须使用 override 修饰符。

C#

abstract class ShapesClass
{
abstract public int Area();
}
class Square : ShapesClass
{
int side = ; public Square(int n)
{
side = n;
}
// Area method is required to avoid
// a compile-time error.
public override int Area()
{
return side * side;
} static void Main()
{
Square sq = new Square();
Console.WriteLine("Area of the square = {0}", sq.Area());
} interface I
{
void M();
}
abstract class C : I
{
public abstract void M();
} }
// Output: Area of the square = 144

override 方法提供从基类继承的成员的新实现。 由 override 声明重写的方法称为重写基方法。 重写的基方法必须与 override 方法具有相同的签名。
不能重写非虚方法或静态方法。 重写的基方法必须是 virtual、abstract 或 override 修饰的。
override 声明不能更改 virtual 方法的可访问性。 override 方法和 virtual 方法必须具有相同的访问级别修饰符。
您不能使用 new、static 或 virtual 修饰符来修改 override 方法。
重写属性声明必须指定与继承属性完全相同的访问修饰符、类型和名称,并且被重写的属性必须是 virtual、abstract 或 override 修饰的。

此示例定义了一个名为 Employee 的基类和一个名为 SalesEmployee 的派生类。 SalesEmployee 类包括一个额外的属性 salesbonus,并重写方法 CalculatePay 以便将该属性考虑在内。
C#

class TestOverride
{
public class Employee
{
public string name; // Basepay is defined as protected, so that it may be
// accessed only by this class and derrived classes.
protected decimal basepay; // Constructor to set the name and basepay values.
public Employee(string name, decimal basepay)
{
this.name = name;
this.basepay = basepay;
} // Declared virtual so it can be overridden.
public virtual decimal CalculatePay()
{
return basepay;
}
} // Derive a new class from Employee.
public class SalesEmployee : Employee
{
// New field that will affect the base pay.
private decimal salesbonus; // The constructor calls the base-class version, and
// initializes the salesbonus field.
public SalesEmployee(string name, decimal basepay,
decimal salesbonus) : base(name, basepay)
{
this.salesbonus = salesbonus;
} // Override the CalculatePay method
// to take bonus into account.
public override decimal CalculatePay()
{
return basepay + salesbonus;
}
} static void Main()
{
// Create some new employees.
SalesEmployee employee1 = new SalesEmployee("Alice",
, );
Employee employee2 = new Employee("Bob", ); Console.WriteLine("Employee4 " + employee1.name +
" earned: " + employee1.CalculatePay());
Console.WriteLine("Employee4 " + employee2.name +
" earned: " + employee2.CalculatePay());
}
}
/*
Output:
Employee4 Alice earned: 1500
Employee4 Bob earned: 1200
*/

最新文章

  1. Java 输出流中的flush方法
  2. android内部培训视频_第一节
  3. C#基础:值类型、引用类型与ref关键字
  4. 关于C转汇编(转自网上)
  5. paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService]
  6. 三维网格去噪算法(bilateral filter)
  7. Android界面实现----PagerTabStrip绚丽的滑动标签
  8. 【转载】在 Visual Studio 2012 中创建 ASP.Net Web Service
  9. java 压缩技术
  10. Apache mod_fcgid fcgid_header_bucket_read函数缓冲区溢出漏洞
  11. 上一篇下一篇 排序 (非ID字段排序)
  12. MEMO:UIButton 中的图片和标题 左对齐
  13. jquery 功能强大的下拉菜单
  14. 输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
  15. vim命令替换操作
  16. Elasticsearch学习笔记(十)批量查询mget、批量增删改bulk
  17. python排列组合之itertools模块
  18. 如何防止Arp攻击
  19. P4053 [JSOI2007]建筑抢修
  20. 基于PHP规范的自动加载方式(composer配置)

热门文章

  1. Codeforces Round #313 (Div. 2) A B C 思路 枚举 数学
  2. ehcache 缓存
  3. request获取json
  4. C/C++程序员必须熟练应用的开源项目(转-清风小阁)
  5. MySql的FIND_IN_SET()查询函数的使用
  6. java_queue
  7. C++泛型编程原理
  8. js里的setTimeout和setInterval之后的页面是空白,阻塞浏览器的document对象,但是不阻塞script方法
  9. javascript语法体系
  10. CSS3弹性盒模型之box-orient & box-direction