C# 从接口方法调用基方法
C# Calling base method from interface method
我开始学习 C#,但不知道如何从接口方法中调用基础 class 方法。在这种特殊情况下,我需要以某种方式从 onRecieve
method.
中调用 sendData
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);
}
Logic.cs
class Logic : WebSocketService
{
public void onReceive(string msg)
{
Console.WriteLine("Received message")
}
}
也许这不是你的意图(评论,我会编辑)但我最简单的事情是扩展基本类型并实现你的接口。
class Logic : WebSocketBase, WebSocketService
{
public void onReceive(string msg)
{
Console.WriteLine("Received message")
// Call method defined in base
sendData(msg);
}
}
如其他评论所述,您没有对 WebSocketBase 做任何事情 class。
其他一些文体要点:
a) WebSocketService 按照惯例应该是 "IWebSocketService"
b) 如果 WebSocketBase 的唯一目的是作为一个基础 class 你应该把它抽象化
c) 一定要标记你的 classes 并用访问修饰符连接它们自己(不仅仅是它们内部的方法)
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")
}
}
看来您的 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);
}
...
}
我开始学习 C#,但不知道如何从接口方法中调用基础 class 方法。在这种特殊情况下,我需要以某种方式从 onRecieve
method.
sendData
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);
}
Logic.cs
class Logic : WebSocketService
{
public void onReceive(string msg)
{
Console.WriteLine("Received message")
}
}
也许这不是你的意图(评论,我会编辑)但我最简单的事情是扩展基本类型并实现你的接口。
class Logic : WebSocketBase, WebSocketService
{
public void onReceive(string msg)
{
Console.WriteLine("Received message")
// Call method defined in base
sendData(msg);
}
}
如其他评论所述,您没有对 WebSocketBase 做任何事情 class。
其他一些文体要点:
a) WebSocketService 按照惯例应该是 "IWebSocketService"
b) 如果 WebSocketBase 的唯一目的是作为一个基础 class 你应该把它抽象化
c) 一定要标记你的 classes 并用访问修饰符连接它们自己(不仅仅是它们内部的方法)
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")
}
}
看来您的 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);
}
...
}