gpt4 book ai didi

java - 在Java中,如果类的父/同级对象没有通过扩展关联,我如何引用它们?

转载 作者:行者123 更新时间:2023-11-30 03:42:07 25 4
gpt4 key购买 nike

我对这个问题真的很困惑,所以请帮助我。网上搜索了一些关于类(class)和父类(class)的描述。但是,我对“父对象”更感兴趣,而不是扩展另一个类(继承)的类。

假设我有以下父类代码:

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;
...
}

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

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

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

然后 child 可以调用该方法并向其 sibling 发送消息:

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

关于java - 在Java中,如果类的父/同级对象没有通过扩展关联,我如何引用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26605989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com