在Java中,如果类的父对象/同级对象没有扩展关系,我该如何引用它们



我对这个话题很困惑,所以请帮帮我。在网上搜索得到了一些关于班级和家长班级的描述。然而,我更感兴趣的是"父对象",而不是扩展另一个类(继承)的类。

假设我有以下Parent类的代码:

public class ParentContainer extends JPanel 
{
    public Child1Container myChild1 = new Child1Container();
    public Child2Container myChild2 = new Child2Container();
    public ParentContainer() {
        JLabel lblParent = new JLabel("I'm the parent");
        add(lblParent);
        add(myChild1);
        add(myChild2);
        JLabel lblParentReceiver = new JLabel("Awaiting message from child...");
        add(lblParentReceiver);
        //I'm going to give child 1 a call...
        JButton btnParentToChild1 = new JButton("Parent: Hello Child 1!");
        btnParentToChild1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JLabel child1Reicever = myChild1.lblChild1Receiver;
                child1Receiver.setText("Parent Says: Hello Child 1?");
            }
        });
        add(btnChild1ToParent);
    }
}

Child1类的以下代码:

public class Child1Container extends JPanel 
{
    public JLabel lblChild1Receiver = new JLabel("Awaiting message from parent...");
    public Child1Container() {
        JLabel lblChild1 = new JLabel("I'm child 1");
        add(lblChild1);
        //I'm waiting for my parent to give me a call...
        add(lblChild1Receiver);
        //I tried to call my parent, but he can't hear me :(
        JButton btnChild1ToParent = new JButton("Child1: Hello Parent!");
        btnChild1ToParent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JLabel parentReceiver = this.getParent().lblParentReceiver;
                parentReceiver.setText("Child1: Hello Parent!");
            }
        });
        add(btnChild1ToParent);
        //I tried to call my sibling, but he can't hear me either :(
        JButton btnChild1ToSibling = new JButton("Child1: Hello Child2!");
        btnChild1ToSibling.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JLabel child2Receiver = getParent().myChild2.lblChild2Receiver;
                child2Receiver.setText("Child1: Hello Child2!");
            }
        });
        add(btnChild1ToSibling);
    }
}

Child2类的以下代码:

public class Child2Container extends JPanel 
{
    public JLabel lblChild2Receiver = new JLabel("Awaiting message from sibling...");
    public Child2Container() {
        JLabel lblChild2 = new JLabel("I'm child 2");
        add(lblChild2);
        //I'm waiting for my sibling to give me a call... I feel so alone :(
        add(lblChild2Receiver);
    }
}

正如你所看到的,这是一个孤独的数字世界。在JPanel容器家族中,除了父母对孩子之外,没有人能够相互交谈。有办法解决这个问题吗?

如果希望子对象(包含的)能够与父对象(容器)通信,可以将父对象的引用传递给子对象的构造函数:

public Child1Container(JPanel parent) {
    ...
    this.parent = parent;
    ...
}

然后子级可以调用父级的任何公共方法。

您可以在ParentContainer类中有一个方法:

public void callSiblings (JPanel from, String message)
{
    // sends message to all the children except of the sender child
}

然后,孩子可以调用该方法并向其兄弟姐妹发送消息:

this.parent.callSiblings (this, "hello brother!");

最新更新