如何确定哪个继承类正在使用抽象类的方法

发布于 2024-08-30 00:30:24 字数 1143 浏览 4 评论 0原文

在我的控制台应用程序中有一个抽象工厂类“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又爬满兰若 2024-09-06 00:30:24

如果您知道要比较的每种类型,请使用 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本身。

If you know each of the types you want to compare against, then use the is operator:

if (this is WorldListener)
{
 // ...
}
else if (this is MasterListener)
{
 // ...
}

Alternatively, you could use GetType if you want a little more flexibility:

var type = GetType();
// Do some logic on the type to determine what to do next.

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:

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);
    }
}

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 appropriate Decorate method; there's no need to modify Listener itself.

梦中的蝴蝶 2024-09-06 00:30:24

您可以使用

if (this is WorldListener)

伪代码来代替

if (inheriting class == WorldListener) 

但是,这样做是一种糟糕的设计味道。您应该强烈考虑替代解决方案,例如,在虚拟方法中执行对控制台包装器的写入,而不是在基类与其子类之间添加这种强耦合。

You can use

if (this is WorldListener)

instead of your pseudocode

if (inheriting class == WorldListener) 

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.

染墨丶若流云 2024-09-06 00:30:24

嗯..好吧,在您的简化示例中,您不会调用 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!");
    }
}

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:

[World] Did something
[World] I did sth specific!
[Master] Did something
[Master] I did sth specific!

The code:

    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!");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文