[英]C#: Interface and Abstract to enable Implement and Override
我必須設計大約5個不同的課程。
這些類的許多部分將非常相似,但它們會有輕微的差異(當然)。
如果我寫的interface
,每個我設計的類的實現,那么每個abstract
的方法interface
必須完全寫入。
如果我編寫一個我設計的每個類派生的base
類,那么每個類將自動在我在base
類中創建的virtual
方法中具有一些功能,但是它們都需要被覆蓋,調用base
功能,包括細微的差異。
有沒有辦法結合這些功能? 我喜歡要求實現interface
的方法,但我也喜歡設置由base
類預編程的功能。
我在這里和其他地方看過很多例子,但看起來並沒有人像我所描述的那樣。 它甚至可能嗎?
編輯:所以,鑒於此:
abstract class Base
{
virtual protected void OptionallyOverridable() {}
abstract protected void SubclassMustImplement();
}
...有沒有辦法寫出這樣的東西:
abstract class Base2
{
DataEventType EventType;
DataChangedEventHandler OnDataChange;
virtual protected void OptionallyOverridable() {}
abstract protected void SubclassMustImplement() {
// values here are guaranteed
}
}
C#允許類從基類繼承並實現接口,那么為什么不這樣做呢?
public interface IFoo
{
public void Bar();
public bool Baz();
}
public abstract class BaseFoo : IFoo
{
//fields/properties used in all classes
public void Bar()
{ //Implementation }
public bool Baz()
{ //Implementation }
}
public class DerivedFoo : BaseFoo, IFoo {...}
AllenG得到了我的加號,但由於我已經輸入了這個,這里有一個更長,更明確的概念演示......
定義你的界面......
public interface IFoo
{
void DoIt();
void DoItWithoutDefaultBehavior();
}
定義一個實現具有某些默認行為的接口的基類,或者某些方法可能沒有默認行為......
public abstract class BaseFoo : IFoo
{
public virtual void DoIt()
{
// Base behavior
Console.WriteLine("base");
}
// This one has no base behavior
public abstract void DoItWithoutDefaultBehavior();
}
您的某些子類可能會使用這些默認行為......
public class DerivedFoo1 : BaseFoo, IFoo
{
// Doesn't override DoIt, takes BaseFoo
public override void DoItWithoutDefaultBehavior()
{
Console.WriteLine("foo1");
}
}
其他人可能會加入他們......
public class DerivedFoo2 : BaseFoo, IFoo
{
public override void DoIt()
{
base.DoIt();
// Additional stuff
Console.WriteLine("derived");
}
public override void DoItWithoutDefaultBehavior()
{
Console.WriteLine("foo2");
}
}
您可以通過類或接口調用方法。
void Main()
{
var foo1 = new DerivedFoo1();
foo1.DoIt();
var foo2 = new DerivedFoo2();
foo2.DoIt();
IFoo foo1AsFoo = new DerivedFoo1();
foo1AsFoo.DoIt();
IFoo foo2AsFoo = new DerivedFoo2();
foo2AsFoo.DoIt();
}
另請參閱: Eric Lippert的文章 ,討論DerivedFoo1
和DerivedFoo2
是否應重述他們在BaseFoo
已經這樣說時實現IFoo
。 為了明確起見,他們在我的例子中做了。
編輯絕對可以在界面中添加事件。 我對我的例子進行了以下更改。 您可以進行類似的更改:
interface IFoo
,添加event EventHandler TheEvent;
BaseFoo
,添加public event EventHandler TheEvent;
BaseFoo.DoIt
,添加if (TheEvent != null) { var args = new EventArgs(); TheEvent(this, args); }
if (TheEvent != null) { var args = new EventArgs(); TheEvent(this, args); }
public void TheHandler(object sender, EventArgs e) { Console.WriteLine("Fired."); }
public void TheHandler(object sender, EventArgs e) { Console.WriteLine("Fired."); }
Main
,添加foo1.TheEvent += TheHandler;
輸出現在變成了
base
Fired.
base
derived
base
base
derived
是的,你已經回答了自己。 你會做兩件事:
你可以混合使用界面和抽象,我想你想要的是這樣的
public interface IFoo
{
void ImplementMe();
int ImplementId { get; }
}
public abstract class BaseFoo : IFoo
{
public virtual void ImplementMe()
{
CommonStuff();
}
public abstract int ImplementId { get; }
private void CommonStuff()
{
// ... do stuff ?
}
}
public sealed class FooImplementationA : BaseFoo
{
public override int ImplementId { get { return 0; } }
public override void ImplementMe()
{
MoreStuff();
base.CommonStuff();
}
private void MoreStuff()
{
// ... do more stuff.
}
}
public sealed class FooImplementationB : BaseFoo
{
public override int ImplementId { get { return 1; } }
public override void ImplementMe()
{
DifferentStuff();
base.CommonStuff();
}
private void DifferentStuff()
{
// ... do different stuff.
}
}
但是你再也不需要界面了,可以簡單地使用BaseFoo
抽象類
'abstract'關鍵字是您正在尋找的。
abstract class Base
{
virtual protected void SubclassCanCallAndOptionallyOverride() {}
abstract protected void SubclassMustBeImplement();
}
有關更多詳細信息,請訪問: http : //msdn.microsoft.com/en-us/library/ms173150.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.