2 - Class "Sink" class Sink { public string name; public int sinkID; public int sinkX; public int sinkY; public List<Packet> queue1 = new List<Packet>(); public List<DateTime> queue2 = new List<DateTime>(); public Sink(string name, int Id , int xLocation, int yLocation) { this.name = name; this.sinkID = Id; this.sinkX = xLocation; this.sinkY = yLocation; } public void insert(Packet packet, DateTime receivingTime) { queue1.Add(packet); queue2.Add(receivingTime); } } 任意想法,我需要你的建议和帮助,因为我不知道如何在这些对象(传感器节点)之间以及传感器节点和接收器之间传递信息。在C#中负责此应用程序的库是什么? 我尝试了什么: 开始节点之间的通信(事件)Any idea, I need your suggestions and your help as I do not have an idea how to pass information between these objects (sensor nodes) and between the sensor nodes and the sink. What is the library which is responsible for this application in C#?What I have tried:To start communication between nodes (events)推荐答案 NotPolitcallyCorrect写的是正确的。也许下面的内容可能会给你一些起点。 What NotPolitcallyCorrect wrote is right. Perhaps the following might give you some starting point. void Main(){var s1 = new SensorNode(1, "don't care", 0, 0);var s2 = new SensorNode(2, "don't care", 0, 0);var sink = new Sink("don't care", 3, 0, 0);sink.AddSensor(s1);sink.AddSensor(s2);s1.SendNewData();s2.SendNewData();s1.SendNewData();}public class EventArgs<T> : EventArgs{private readonly T _value;public EventArgs(T value){_value = value;}public T Value{get { return _value; }}}public class Packet{ public int SensorId { get; set; }public string theData { get; set; }public Packet(int id, string data){SensorId = id;theData = data;}}public interface ISensorNode{ event EventHandler<EventArgs<Packet>> DataSend;}class SensorNode : ISensorNode{public event EventHandler<EventArgs<Packet>> DataSend;public int Id { get; set; }public SensorNode(int id,string name,int depth, double energy){Id = id;}public void SendNewData(){Packet p = new Packet(Id, "123.4");var handler = DataSend;var args = new EventArgs<Packet>(p);handler?.Invoke(this, args);}}class Sink{ public Sink(string name, int Id , int xLocation, int yLocatio){}public void AddSensor(ISensorNode newSensor){newSensor.DataSend += OnDataReceived;}private void OnDataReceived(object sender, EventArgs<Packet> e){Packet packed = e.Value;Console.WriteLine( 从ID为{packed.SensorId}的传感器接收数据{packed.theData}; } } "received data {packed.theData} from sensor with id {packed.SensorId}");}} 这篇关于如何在同一个类和其他类中的不同对象之间传递信息作为C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 14:18