简体   繁体   English

如何从基类调用派生类方法?

[英]How do I call a derived class method from the base class?

I have read several similar questions about this but none seem to solve the problem I am facing. 我已经阅读了几个类似的问题,但似乎没有解决我面临的问题。 The typical answer is to cast as the derived class but I cannot since I do not know the derived class type. 典型的答案是作为派生类进行转换,但我不能因为我不知道派生类类型。

Here is my example: 这是我的例子:

class WireLessDevice { // base class
    void websocket.parsemessage(); // inserts data into the wireless device object
}

class WiFi : WireLessDevice { // derived class
    GPSLoc Loc;
}

Wireless Device can also be derived to make Bluetooth, Wi-Max, Cellular, etc. devices and thus I do not know which type of wirelessdevice will be receiving the data. 还可以导出无线设备以制作蓝牙,Wi-Max,蜂窝等设备,因此我不知道哪种类型的无线设备将接收数据。

When a GPS packet is received on the websocket in the base class I need to update the derived device's location. 当在基类中的websocket上接收到GPS数据包时,我需要更新派生设备的位置。

I thought perhaps sending a message via a queue or creating an event handler and sending the location in the event arguments but those seem a little clunky when the data is staying within the class. 我想也许可以通过队列发送消息或创建事件处理程序并在事件参数中发送位置,但是当数据停留在类中时,这些看起来有点笨拙。

Is there something built into the language that would allow me to call my derived device from the base class without knowing the type? 是否有内置于该语言中的内容可以让我在不知道类型的情况下从基类调用我的派生设备?

The correct way is to add a method DoSomeMagic() in the base class, with default implementation, or abstract. 正确的方法是在基类中添加方法DoSomeMagic(),使用默认实现或抽象。 The derived class should than override it to do its magic. 派生类应该覆盖它来做它的魔力。

Something like this maybe: 这样的事情可能是:

public class WireLessDevice
{ // base class
    protected virtual void ParseMessage()
    {
        // Do common stuff in here
    }
}

public class WiFi : WireLessDevice
{ // derived class
    override void ParseMessage()
    {
        base.ParseMessage();//Call this if you need some operations from base impl.
        DoGPSStuff();
    }
    private void DoGPSStuff()
    {
        //some gps stuff
    }
    GPSLoc Loc;
}

Virtual methods would be a good solution. 虚拟方法将是一个很好的解决方案。 You create a method ParseMessage in the base class which handles all the stuff common to each derived type, then override for specific devices. 您在基类中创建一个方法ParseMessage ,它处理每个派生类型共有的所有内容,然后覆盖特定设备。

In order to still be able to handle other message types, you will need to call the base.ParseMessage as appropriate: 为了仍然能够处理其他消息类型,您需要根据需要调用base.ParseMessage

class WireLessDevice
{
    protected virtual bool ParseMessage(byte[] message)
    {
        // Inspect message and handle the known ones
        return true; // Only if message parsed, otherwise false
    }
}

class WiFi : WireLessDevice
{
    GPSLoc Loc;

    protected override bool ParseMessage(byte[] message)      
    {
        bool messageParsed = base.ParseMessage(message)
        if (!messageParsed)
        {
            // Handle device specific message with GPSLoc
            Loc = ...
        }
        else
        {
            return false;
        }
    }
}

If your base class is not handling any other messages, the code could be simpler (and using abstract ). 如果您的基类没有处理任何其他消息,则代码可能更简单(并使用abstract )。 However, in that case I would consider moving the web socket code to the derived class. 但是,在这种情况下,我会考虑将Web套接字代码移动到派生类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:如何从派生类的静态方法调用基类的静态方法? - C#: How do I call a static method of a base class from a static method of a derived class? 我有一个基类。 如何为正确的派生类调用正确的方法? - I have a base class. How do I call the right method for the right derived class? 我可以调用基类的派生方法吗? - Can I call a derived method from base class? 如何从 C# 中的派生类实例调用基方法? - How to call base method from derived class instance in C#? 如何获取派生方法来更改其基本变量或对象之一,然后从类外部调用更改的对象? - How do I get a derived method to change one of its base variables or objects and then call the changed object from outside the class? 即使当前实例是派生类,我们如何从基类中的另一个方法调用虚方法? - How do we call a virtual method from another method in the base class even when the current instance is of a derived-class? 我应该如何使用从通用基础 class 派生的 class 调用基础 class 构造函数? - How should I call a base class constructor with a class derived from a generic base class? 派生类不调用基类方法 - Derived class does not call base class method 我如何将类(派生自通用“基”类)转换为该通用“基”类 - How do i convert a class (which is derived from a generic "base" class) to that generic "base" class 我如何引用来自基础 class 的派生类型? - how do i refer to the derived type from the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM