1. 程式人生 > 實用技巧 >AutoCAD.Net/C#.Net QQ群:193522571 虛擬方法的使用

AutoCAD.Net/C#.Net QQ群:193522571 虛擬方法的使用

shell類中有一個虛方法;

而其子類Cylinder、Head及其子類中去實現此虛方法;

這樣,在其它類如Main類中可以通過shell及其子類的例項來呼叫此虛方法;

如:new一個HeadEllipse EH

用方法Draw(EH)來實現;

  public class Main
  {
  
public void Draw(Shell shell) { shell.Draw(); } } public class Shell : Part { public virtual ObjectId Draw() {
return ObjectId.Null; } } public class Cylinder : Shell { public override ObjectId Draw() { int identify = this.GetCurrentPartIdentity<Cylinder>() + 1; ObjectId id = Draw(identify); this.SetCurrentPartIdentity<Cylinder>(identify); return id; } }
public class Head : Shell { } public class HeadEllipse : Head { public override ObjectId Draw() { int identify = this.GetCurrentPartIdentity<HeadEllipse>() + 1; ObjectId id = Draw(identify); this.SetCurrentPartIdentity<HeadEllipse>(identify); return
id; } } public class HeadDished : Head { public override ObjectId Draw() { int identify = this.GetCurrentPartIdentity<HeadDished>() + 1; ObjectId id = Draw(identify); this.SetCurrentPartIdentity<HeadDished>(identify); return id; } }