0

我开始学习 C# 并且无法弄清楚如何从接口方法中调用基类方法。在这种特殊情况下,我需要以某种方式sendData从方法内部调用onRecieve

WebSocketBase.cs

using WebSocketSharp;
using System;
using System.Collections.Generic;
using System.Threading;

class WebSocketBase
{  
    private WebSocket webSocketClient = null;
    private WebSocketService webService = null;
    private string url = null;

    public WebSocketBase(string url, WebSocketService webService)
    {
        this.webService = webService;
        this.url = url;
    }

    public void start()
    {
        webSocketClient = new WebSocket(url);
        webSocketClient.OnError += new EventHandler<WebSocketSharp.ErrorEventArgs>(webSocketClient_Error);
        webSocketClient.OnOpen += new EventHandler(webSocketClient_Opened);
        webSocketClient.OnClose += new EventHandler<WebSocketSharp.CloseEventArgs>(webSocketClient_Closed);
        webSocketClient.OnMessage += new EventHandler<MessageEventArgs>(webSocketClient_MessageReceived);
        webSocketClient.ConnectAsync();
    }

    private void webSocketClient_MessageReceived(object sender, MessageEventArgs e)
    {
        webService.onReceive(e.Data);
    }

    public void send(string channel)
    {
        webSocketClient.Send(channel);
    }

    public void sendData(string data)
    {
        send("data");
    }

}

interface WebSocketService
{
    void onReceive(String msg);
}

逻辑.cs

class Logic : WebSocketService
{
    public void onReceive(string msg)
    {
         Console.WriteLine("Received message")
    }
}
4

3 回答 3

3

也许这不是你的意图(评论,我会编辑),但我最简单的事情是扩展基本类型并实现你的接口。

class Logic : WebSocketBase, WebSocketService
{
    public void onReceive(string msg)
    {
         Console.WriteLine("Received message")

         // Call method defined in base
         sendData(msg);
    }
}
于 2016-08-02T15:58:11.603 回答
2

正如其他评论所指出的,您没有对 WebSocketBase 类做任何事情。

其他一些风格点:
a)WebSocketService 按照惯例应该是“IWebSocketService”
b)如果 WebSocketBase 的唯一目的是成为一个基类,你应该将它抽象
c)确保也用访问修饰符标记你的类和接口本身(不是只是其中的方法)

public interface IWebSocketService
{
   void onReceive(String msg);
}

public abstract class WebSocketBase : IWebSocketService
{

...


private void webSocketClient_MessageReceived(object sender, MessageEventArgs e)
{
    webService.onReceive(e.Data);
}

  public void sendData(string data)
  {
      send("data");
  }

}

public class Logic : WebSocketBase
{
    public void onReceive(string msg)
    {
         Console.WriteLine("Received message")
    }
}
于 2016-08-02T16:00:57.550 回答
1

所以看起来你WebSocketBase对服务有依赖。为什么不让服务知道WebSocketBase发送消息的内容是什么?

interface WebSocketService
{
    void onReceive(WebSocketBase sender, String msg);
}

class Logic : WebSocketService
{
    public void onReceive(WebSocketBase sender, string msg)
    {
         sender.send("blah");
    }
}

class WebSocketBase
{  
    ...

    private void webSocketClient_MessageReceived(object sender, MessageEventArgs e)
    {
        webService.onReceive(this, e.Data);
    }

    public void send(string channel)
    {
        webSocketClient.Send(channel);
    }

    ...    
}
于 2016-08-02T16:09:55.050 回答