类的ArrayList,获取在另一个类中创建的特定值

时间:2016-08-29 07:47:57

标签: java arraylist

可能是标题不是具体的,我只是不知道怎么称呼它。我会详细解释你

我有这些课程:

public class ChannelComponent {

    private String name;
    private String mode; //(1P1C / XPXC / 1PXC)
    private List<SourceProvidedPort> publishers = new ArrayList<SourceProvidedPort>();
    private List<SinkRequiredPort> subscribers = new ArrayList<SinkRequiredPort>();

    public ChannelComponent(String name, String mode) {
        this.name = name;
        this.mode = mode;
    }

    public boolean canISubscribe(SinkRequiredPort newPort) {
        if ((mode.equals("1P1C") || mode.equals("1PXC")) && subscribers.size() < 1) {
            subscribers.add(newPort);
            return true;
        } else if (mode.equals("XPXC")) {
            subscribers.add(newPort);
            return true;
        }
        return false;
    }

    public String getName() {
        return name;
    }

    public String getMode() {
        return mode;
    }

    public void printChannel() {
        System.out.println("[" + name + "," + mode + "]" + "\n");
    }

}

TestCentralRegistry

public class TestCentralRegistry {

    private List<ChannelComponent> channels = new ArrayList<ChannelComponent>();

    public void addChannelComponent(ChannelComponent c) {
        channels.add(c);
    }


    public static void main(String... args) {
        TestCentralRegistry demo = new TestCentralRegistry();

        demo.addChannelComponent(new ChannelComponent("channel1", "1P1C"));
        demo.addChannelComponent(new ChannelComponent("channel2", "XPXC"));

    }
}

TestCentralRegistry 类中,我创建了2个channelComponents,这些频道我想比较方法 canISubscribe 中的模式值(位于 ChannelComponent 类)。但是,为什么我可以检索 TestCentralRegistry 中创建的值,以便在 ChannelComponent 类中读取它们?

我错过了什么?

因为,从另一个类 TestChannel 我将有一个 ChannelComponent 引用,调用方法 canISubscribe

public class TestChannel {
    ChannelComponent channelComponent;

    public void callSubscribe(SinkRequiredPort newPort){
        channelComponent.canISubscribe(newPort);
    }

    public static void main(String... args) {
        TestChannel testChannel = new TestChannel();
        SinkRequiredPort sinkPort = new SinkRequiredPort();
        sinkPort.setWantsUse("channel1");

        testChannel.callSubscribe(sinkPort);

    }   
}

我需要比较在 TestCentralRegistry TestChannel 中创建的值,看看是否存在匹配。我知道我仍然需要添加一些行,比如从 newPort.getWantsUse(); 获取值并将其与channelComponent name 进行比较......但我仍然需要 TestCentralRegistry

中创建的值

我希望我的问题很明确 有什么建议? 提前谢谢

1 个答案:

答案 0 :(得分:2)

尝试在ChannelComponent中持有对TestCentralRegistry的引用。

public class ChannelComponent {

    private String name;
    private String mode; //(1P1C / XPXC / 1PXC)
    private List<SourceProvidedPort> publishers = new ArrayList<SourceProvidedPort>();
    private List<SinkRequiredPort> subscribers = new ArrayList<SinkRequiredPort>();
    private TestCentralRegistry testCentralRegistry;

    public ChannelComponent(String name, String mode) {
        this.name = name;
        this.mode = mode;
    }

    public void registerTestCentralRegistry( TestCentralRegistry testCentralRegistry) {
        this.testCentralRegistry = testCentralRegistry;
    }
}

注册您的TestCentralRegistry,如下所示:

public class TestCentralRegistry {

    private List<ChannelComponent> channels = new ArrayList<ChannelComponent>();

    public void addChannelComponent(ChannelComponent c) {
        channels.add(c);
    }

    public static void main(String... args) {
        TestCentralRegistry demo = new TestCentralRegistry();

        ChannelComponent cc1 = new ChannelComponent("channel1", "1P1C");
        cc1.registerTestCentralRegistry( demo);
        ChannelComponent cc2 = new ChannelComponent("channel2", "XPXC");
        cc2.registerTestCentralRegistry( demo);

        demo.addChannelComponent( cc1);
        demo.addChannelComponent( cc2);
    }
}

然后,您可以通过从ChannelComponent调用testCentralRegistry.getX()来检索在TestCentralRegistry中创建的值。