第四章 继承
4.1 继承
4.2 继承的类型
1 实现继承和接口继承
2 多重继承
3 结构和类
结构支持接口继承,结构总是派生自System.ValueType,它们还可以派生自任意多个接口。
类总是派生自System.Object或用户选择的另一个类,它们还可以派生自任意多个接口。
4.3 实现继承
4.3.1 虚方法
class MyBaseClass { public virtual string VirtualMethod() { return "this is a virtual method"; } } class MyDerivedClass: MyBaseClass { public override string VirtualMethod() { return "this is an override method"; } }
4.3.2 隐藏方法
class MyDrivedClass : HisBaseClass { public new int MyGroovyMethod() { return 0; } }
4.3.3 调用函数的基类版本
return base.CalculatePrice() * 0.94;
4.3.4 抽象类和抽象函数
抽象函数是虚拟的但是不能提供virtual关键字,否则会产生错误。
abstract class Building { public abstract decimal CalculateHeatingCost(); }
4.3.5 密封类和密封方法
sealed class FinalClass
{
}
class MyClass : MyBaseClass
{
public sealed override void FinalMethod();
}
4.3.6 派生类的构造函数
public Nevermore60Customer(string name) : base(name)
4.4 修饰符
4.4.1 可见性修饰符
public; protected; internal; private; protected internal
4.4.2 其他修饰符
new; static; virtual; abstract; override;sealed;extern
4.5 接口
public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance{get;} }
接口只能包含方法 属性 索引器和事件的声明。
4.5.2 派生的接口
public interface ITransferBandAccount : IBankAccount { bool TransferTo(IBankAccount destination, decimal amount); }
posted on 2014-07-21 20:08 mszhouzhiyong 阅读(90) 评论(0) 收藏 举报
浙公网安备 33010602011771号