Java:为Class Type参数添加什么?

时间:2022-05-24 16:12:02

I am working on simulating wireless sensor network. I have to create a nodes in the simulation.

我正在研究模拟无线传感器网络。我必须在模拟中创建一个节点。

Functions to create nodes is defined in class Simulator. Here Simulator and RadioModel are two user defined classes.

创建节点的函数在类Simulator中定义。这里Simulator和RadioModel是两个用户定义的类。

For creating a node , I have a pre-defined function i.e. createNode() defined within Simulator class. It has following syntax:

为了创建节点,我有一个预定义的函数,即在Simulator类中定义的createNode()。它具有以下语法:

Node createNode(Class nodeClass, RadioModel radioModel, int nodeId, 
                double x, double y, double z)

Here I am fine with RadioModel parameter and all other arguments except "Class nodeClass" are working fine. What to substitute for this argument i.e. nodeclass?

这里我对RadioModel参数很好,除了“Class nodeClass”之外的所有其他参数都运行正常。用什么代替这个参数,即nodeclass?

Any help will be of great use....

任何帮助都会有很大用处......


Yes dacwe, as you said Node class is extended by Mica2Node class which I am instantiating. And I have passed the argument as
sim1.createNode(Mica2Node.class, g, nodeId,x,y,z);

是的dacwe,正如你所说的Node类是由我实例化的Mica2Node类扩展的。我已经将参数传递为sim1.createNode(Mica2Node.class,g,nodeId,x,y,z);

Here sim1 is an object of class Simulator and g is an object of class RadioModel. Beacause createNode is not a static method, I called it through object. But I am facing three other warnings: from other class i,e. Application.java

这里sim1是类Simulator的对象,g是RadioModel类的对象。因为createNode不是静态方法,我通过对象调用它。但我面临其他三个警告:来自其他类别,e。 Application.java

public Application(Node node){
    this.node = node;
    node.addApplication(this);
}  

here it is showing error in addApplication(this) method. This method is defined in Node.java as below:

这里它在addApplication(this)方法中显示错误。此方法在Node.java中定义如下:

public void addApplication(Application app){
    app.nextApplication = firstApplication;
    firstApplication = app; } 

And the error is as below:

错误如下:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: ()void at net.tinyos.prowler.Application.(Application.java:31) at net.tinyos.prowler.TestBroadcastNode$BroadcastApplication.(TestBroadcastNode.java:36) at net.tinyos.prowler.TestBroadcastNode.main(TestBroadcastNode.java:118)

线程“main”中的异常java.lang.RuntimeException:无法编译的源代码 - 错误的sym类型:()void net.tinyos.prowler.Application。(Application.java:31)net.tinyos.prowler.TestBroadcastNode $ BroadcastApplication。 (TestBroadcastNode.java:36)在net.tinyos.prowler.TestBroadcastNode.main(TestBroadcastNode.java:118)

Please help me out......

请帮帮我......

2 个解决方案

#1


0  

Are you looking for something like this?

你在找这样的东西吗?

public <T extends Node> T createNode(Class<T> clazz, RadioModel radioModel, 
            int nodeId, double x, double y, double z);

Which can be used like

哪个可以用

RadioModel radioModel = ... 
MyNode node = createNode(MyNode.class, radioModel, 1, 2, 3);

Another approach might be to create a factory which encapsulates the creation of all node types, e.g.,

另一种方法可能是创建一个封装所有节点类型创建的工厂,例如,

public interface NodeFactory {
     public MyNode createMyNode(..);
     public YourNode createYourNode(..);
}

#2


0  

It is (proably) whay kind of class the createNode method should create.

它(可能)是createNode方法应该创建的类。

You have some "node classes" that extend Node (in the example below BigNode and SomeOtherNode). So you use the class information to create one of those.

您有一些扩展Node的“节点类”(在BigNode和SomeOtherNode下面的示例中)。因此,您使用类信息来创建其中一个。

The calling part:

呼叫部分:

 Node created = Simulator.createNode(BigNode.class, radioModel, nodeId, x, y, z);

The implementation of createNode:

createNode的实现:

Node createNode(Class nodeClass, RadioModel radioModel, int nodeId, 
                double x, double y, double z) {

    if (nodeClass.equals(BigNode.class))
        return new BigNode(radioModel, nodeId, x, y, z);

    if (nodeClass.equals(SomeOtherNode.class)) {
        return new SomeOtherNode(.....);

    throw new IllegalArgumentException("could not create node of type " + 
                                       nodeClass);
}

#1


0  

Are you looking for something like this?

你在找这样的东西吗?

public <T extends Node> T createNode(Class<T> clazz, RadioModel radioModel, 
            int nodeId, double x, double y, double z);

Which can be used like

哪个可以用

RadioModel radioModel = ... 
MyNode node = createNode(MyNode.class, radioModel, 1, 2, 3);

Another approach might be to create a factory which encapsulates the creation of all node types, e.g.,

另一种方法可能是创建一个封装所有节点类型创建的工厂,例如,

public interface NodeFactory {
     public MyNode createMyNode(..);
     public YourNode createYourNode(..);
}

#2


0  

It is (proably) whay kind of class the createNode method should create.

它(可能)是createNode方法应该创建的类。

You have some "node classes" that extend Node (in the example below BigNode and SomeOtherNode). So you use the class information to create one of those.

您有一些扩展Node的“节点类”(在BigNode和SomeOtherNode下面的示例中)。因此,您使用类信息来创建其中一个。

The calling part:

呼叫部分:

 Node created = Simulator.createNode(BigNode.class, radioModel, nodeId, x, y, z);

The implementation of createNode:

createNode的实现:

Node createNode(Class nodeClass, RadioModel radioModel, int nodeId, 
                double x, double y, double z) {

    if (nodeClass.equals(BigNode.class))
        return new BigNode(radioModel, nodeId, x, y, z);

    if (nodeClass.equals(SomeOtherNode.class)) {
        return new SomeOtherNode(.....);

    throw new IllegalArgumentException("could not create node of type " + 
                                       nodeClass);
}