如何确定哪个继承类正在使用抽象类的方法
在我的控制台应用程序中有一个抽象工厂类“Listener”,其中包含用于侦听和接受连接以及生成客户端类的代码。该类由另外两个类(WorldListener 和 MasterListener)继承,它们包含更多特定于协议的覆盖和函数。
我还有一个帮助程序类 (ConsoleWrapper),它封装并扩展了 System.Console,其中包含用于写入有关 WorldListener 和 MasterListener 实例所发生情况的控制台信息的方法。
我需要一种方法来确定抽象 ListenerClass 中哪个继承类正在调用其方法。
对于此问题的任何帮助将不胜感激!我很困惑: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();
}
预期产出
[世界]做了一些事情!
[世界]我做了一些具体的事情!
[师父]做了一些事情!
[世界] 我做了一些具体的事情!
In my console application have an abstract Factory class "Listener" which contains code for listening and accepting connections, and spawning client classes. This class is inherited by two more classes (WorldListener, and MasterListener) that contain more protocol specific overrides and functions.
I also have a helper class (ConsoleWrapper) which encapsulates and extends System.Console, containing methods for writing to console info on what is happening to instances of the WorldListener and MasterListener.
I need a way to determine in the abstract ListenerClass which Inheriting class is calling its methods.
Any help with this problem would be greatly appreciated! I am stumped :X
Simplified example of what I am trying to do.
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();
}
Expected output
[World] Did something!
[World] I did something specific!
[Master] Did something!
[World] I did something specific!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您知道要比较的每种类型,请使用
is
运算符:或者,如果您想要更多的灵活性,您可以使用
GetType
:您应该不过,要谨慎使用这种方法;这通常表明设计不好,您需要明确检查类型(正如这些可爱的人所坚持的那样)。相反,使用多态性将所需的行为委托给基类(在基类中使用虚拟或抽象方法)几乎总是更合适——毕竟,这就是它的设计目的!
您可以应用像这样的多态性:
这将产生输出
[World] Did Something!
。这种方法的优点是,如果您想添加另一种类型的侦听器,只需使用适当的Decorate
方法为其定义一个新类即可;无需修改Listener
本身。If you know each of the types you want to compare against, then use the
is
operator:Alternatively, you could use
GetType
if you want a little more flexibility:You should be careful with this approach, however; it's generally indicative of bad design that you need to explicitly check for types (as these lovely people insist). Instead, it's almost always more appropriate to use polymorphism to delegate the desired behaviour to the base class (using a virtual or abstract method in the base class) – this is, after all, what it's designed for!
You might apply polymorphism something like this:
This will produce the output
[World] Did something!
. The advantage of this approach is that if you ever want to add another type of listener, it's simply a matter of defining a new class for it with the appropriateDecorate
method; there's no need to modifyListener
itself.您可以使用
伪代码来代替
但是,这样做是一种糟糕的设计味道。您应该强烈考虑替代解决方案,例如,在虚拟方法中执行对控制台包装器的写入,而不是在基类与其子类之间添加这种强耦合。
You can use
instead of your pseudocode
However, doing this is a bad design smell. You should strongly consider an alternative solution, e.g. performing the write to the console wrapper in a virtual method instead of adding this strong coupling between the base class and its subclasses.
嗯..好吧,在您的简化示例中,您不会调用 DoSomething() 和 DoSomethingSpecific(),并且 MasterListener 没有实现。
另外,如果我理解正确的话,在您的预期输出中,您的 MasterListener.DoSomethingSpecific() 运行 ConsoleWrapper.WorldWrite ..您可能是指 MasterWrite ?
无论如何..这是一个可以执行您想要的操作的示例(至少以我理解您的请求的方式:P)
打印的结果是:
代码:
Hmm.. Well, in your simplified example you don't call DoSomething() and DoSomethingSpecific(), and there's no implementation for MasterListener..
Also, if I understand it right, in your expected output your MasterListener.DoSomethingSpecific() runs a ConsoleWrapper.WorldWrite.. You probably meanr MasterWrite?
In any case.. Here's a working example that does what you want (at least in the way I understood your request :P )
The printed result is:
The code: