First off this is a pretty basic problem, but i just cant seem to get it, ok so here is a general overview of my program
首先这是一个非常基本的问题,但我似乎无法得到它,好吧所以这里是我的程序的一般概述
|class A |
|new B();|
|new C();|
/ \
/ \
|Class B | |Class C |
|new D();| |method E(){}|
|
|Class D |
|Invokes method E|
This is my programs hierarchy and I want to invoke the non static method E from class D, without creating a new instance of class C, is there a way to do this or do i have to restructure my class hierarchy.
这是我的程序层次结构,我想从类D调用非静态方法E,而不创建类C的新实例,有没有办法做到这一点,或者我必须重构我的类层次结构。
**EDIT:**Here is real code to display my problem(without a class B though):
**编辑:**这是显示我的问题的真实代码(虽然没有B类):
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class TheFrame extends JFrame{
public TheFrame(){
setTitle("Suduku Solver");
add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
new TextLabel(), new ChangeButton()));
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(300,300);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new TheFrame();
}
}
import javax.swing.JLabel;
public class TextLabel extends JLabel{
public TextLabel(){
setText("This is the Label");
}
public void ChangeLabel(){
setText("Changed Label");
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ChangeButton extends JButton {
public ChangeButton(){
setText("Click to change Label");
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//Invoke TextLabel changeText method here
}
});
}
}
i would like to invoke the changeText method by clicking the button
我想通过单击按钮调用changeText方法
2 个解决方案
#1
1
You can't invoke a non-static method without an instance. That's "non negotiable".
没有实例,您无法调用非静态方法。这是“不可谈判的”。
You'll have to rework your class structure.
你必须重写你的班级结构。
#2
1
You could inject C
class to D
using constructor or setters. This is an example with constructor injection.
您可以使用构造函数或setter将C类注入到D中。这是构造函数注入的示例。
class A {
A() {
C c = new C();
B b = new B(c);
}
}
class B {
B(C c) {
D d = new D(C c);
}
}
class D {
private C c;
D(C c) {
this.c = c;
}
public void methodThatCallsE() {
c.E();
}
}
This way you can call method E
without having to create an object of C
in D
.
这样您就可以调用方法E而无需在D中创建C的对象。
You could modify your ChangeButton
class to receive any JLabel
object.
您可以修改ChangeButton类以接收任何JLabel对象。
public class ChangeButton extends JButton {
private JLabel jlabel;
public ChangeButton(JLabel jlabel){
this.jlabel = jlabel;
setText("Click to change Label");
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
jlabel.setText("new Text");
}
});
}
}
And then, TheFrame
class would look like this.
然后,TheFrame类看起来像这样。
public class TheFrame extends JFrame{
public TheFrame(){
setTitle("Suduku Solver");
JLabel jlabel = new TextLabel();
add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
jlabel, new ChangeButton(jlabel)));
...
#1
1
You can't invoke a non-static method without an instance. That's "non negotiable".
没有实例,您无法调用非静态方法。这是“不可谈判的”。
You'll have to rework your class structure.
你必须重写你的班级结构。
#2
1
You could inject C
class to D
using constructor or setters. This is an example with constructor injection.
您可以使用构造函数或setter将C类注入到D中。这是构造函数注入的示例。
class A {
A() {
C c = new C();
B b = new B(c);
}
}
class B {
B(C c) {
D d = new D(C c);
}
}
class D {
private C c;
D(C c) {
this.c = c;
}
public void methodThatCallsE() {
c.E();
}
}
This way you can call method E
without having to create an object of C
in D
.
这样您就可以调用方法E而无需在D中创建C的对象。
You could modify your ChangeButton
class to receive any JLabel
object.
您可以修改ChangeButton类以接收任何JLabel对象。
public class ChangeButton extends JButton {
private JLabel jlabel;
public ChangeButton(JLabel jlabel){
this.jlabel = jlabel;
setText("Click to change Label");
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
jlabel.setText("new Text");
}
});
}
}
And then, TheFrame
class would look like this.
然后,TheFrame类看起来像这样。
public class TheFrame extends JFrame{
public TheFrame(){
setTitle("Suduku Solver");
JLabel jlabel = new TextLabel();
add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
jlabel, new ChangeButton(jlabel)));
...