[英]How to determine which inheriting class is using an abstract class's methods
在我的控制台應用程序中有一個抽象的Factory類“Listener”,它包含用於偵聽和接受連接以及生成客戶端類的代碼。 此類由另外兩個類(WorldListener和MasterListener)繼承,這些類包含更多特定於協議的覆蓋和函數。
我還有一個輔助類(ConsoleWrapper),它封裝和擴展System.Console,包含用於寫入控制台信息的方法,用於解釋WorldListener和MasterListener實例的內容。
任何有關此問題的幫助將不勝感激! 我很難過:X
我想要做的簡化示例。
abstract class Listener
{
public void DoSomething()
{
if(inheriting class == WorldListener)
ConsoleWrapper.WorldWrite("Did something!");
if(inheriting class == MasterListener)
ConsoleWrapper.MasterWrite("Did something!");
}
}
public static ConsoleWrapper
{
public void WorldWrite(string input)
{
System.Console.WriteLine("[World] {0}", input);
}
}
public class WorldListener : Listener
{
public void DoSomethingSpecific()
{
ConsoleWrapper.WorldWrite("I did something specific!");
}
}
public void Main()
{
new WorldListener();
new MasterListener();
}
預期產出
[世界]有所作為!
[世界]我做了一些具體的事!
[大師]做了些什么!
[世界]我做了一些具體的事!
如果您知道要比較的每種類型,請使用is
運算符:
if (this is WorldListener)
{
// ...
}
else if (this is MasterListener)
{
// ...
}
或者,如果您想要更多靈活性,可以使用GetType
:
var type = GetType();
// Do some logic on the type to determine what to do next.
但是,你應該小心這種方法; 它通常表明你需要明確檢查類型的糟糕設計(正如這些可愛的人堅持)。 相反,使用多態來將所需行為委托給基類(使用基類中的虛擬或抽象方法)幾乎總是更合適 - 畢竟,這就是它的設計目標!
你可以應用這樣的多態:
static class Program
{
static void Main(string[] args)
{
Listener listener = new WorldListener();
listener.DoSomething();
}
}
abstract class Listener
{
public void DoSomething()
{
var output = Decorate("Did something!");
ConsoleWrapper.WriteLine(output);
}
protected abstract string Decorate(string input);
}
class WorldListener : Listener
{
protected override string Decorate(string input)
{
return string.Format("[World] {0}", input);
}
}
class MasterListener : Listener
{
protected override string Decorate(string input)
{
return string.Format("[Master] {0}", input);
}
}
這將產生輸出[World] Did something!
。 這種方法的優點是,如果你想要添加另一種類型的監聽器,只需用適當的Decorate
方法為它定義一個新類; 沒有必要修改Listener
本身。
嗯..在你的簡化示例中,你不要調用DoSomething()和DoSomethingSpecific(),並且沒有MasterListener的實現。另外,如果我理解正確,在你的預期輸出中你的MasterListener.DoSomethingSpecific()運行一個ConsoleWrapper.WorldWrite ..你可能意味着MasterWrite?
在任何情況下..這是一個做你想要的工作例子(至少以我理解你的要求的方式:P)
打印結果是:
[World] Did something
[World] I did sth specific!
[Master] Did something
[Master] I did sth specific!
代碼:
void Main()
{
var wl = new WorldListener();
wl.DoSomething();
wl.DoSpecific();
var ml = new MasterListener();
ml.DoSomething();
ml.DoSpecific();
}
public abstract class Listener
{
public abstract string Category { get; }
public void DoSomething()
{
ConsoleWrapper.Write(Category, "Did something");
}
}
public static class ConsoleWrapper
{
public static void Write(string category, string input)
{
Console.WriteLine("[{0}] {1}", category, input);
}
}
public class WorldListener : Listener
{
public override string Category { get { return "World"; } }
public void DoSpecific()
{
ConsoleWrapper.Write(Category, "I did sth specific!");
}
}
public class MasterListener : Listener
{
public override string Category { get { return "Master"; } }
public void DoSpecific()
{
ConsoleWrapper.Write(Category, "I did sth specific!");
}
}
您可以使用
if (this is WorldListener)
而不是你的偽代碼
if (inheriting class == WorldListener)
然而,這樣做是一個糟糕的設計氣味。 您應該強烈考慮替代解決方案,例如,在虛方法中執行對控制台包裝器的寫入,而不是在基類及其子類之間添加這種強耦合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.