有没有attribute或pattern告诉编译器不允许覆盖可覆盖的方法?
例如:
Vehicle
public class Vehicle
{
public virtual void Start() { }
}
Run Code Online (Sandbox Code Playgroud)
Car
public class Car : Vehicle
{
// ################
[DontAllowOverrideAgain] //I need something like this attribute
// ################
public override void Start()
{
// Todo => codes that every car must invoke before start ...
CarStart();
// Todo => codes that every car must invoke after start ...
}
public virtual void CarStart() { }
}
Run Code Online (Sandbox Code Playgroud)
CoupeCar
public class CoupeCar : Car
{
// throw and error or show a message to developer
public override void Start() { }
public override void CarStart() { }
}
Run Code Online (Sandbox Code Playgroud)
当然,只需创建第一个覆盖sealed,这将导致开发人员可以看到的编译时失败
public class Car : Vehicle
{
public sealed override void Start()
{
// Todo => codes that every car must invoke before start ...
CarStart();
// Todo => codes that every car must invoke after start ...
}
public virtual void CarStart() { }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |