无法访问 main 方法之外的类实例方法



我正在阅读"Head first Java"一书,并开始了第12章。

当我尝试创建方法时changeButtonText()我无法从button访问任何类方法。

这是为什么呢?我在这段代码中做错了什么?

import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton button = new JButton("Click Me");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(300,300);
frame.setVisible(true);

}
public void changeButtonText(){
button.setText("I've been clicked");
}
}

无法访问变量的原因是因为它的作用域(关于变量作用域的教程:https://www.baeldung.com/java-variable-scope(。

main方法中声明变量JButton button,因此它无法在其外部的任何位置访问,即使在main调用自身的方法中也是如此。

若要使changeButtonText知道button变量存在,必须将其作为此方法的参数传递:

import javax.swing.*;
public class Main {
public static void main(String[] args) {
JButton button = new JButton("Click Me");
changeButtonText(button);
}
public static void changeButtonText(JButton button){
button.setText("I've been clicked");
}
}

我还在changeButtonText方法前面添加了static关键字,因为main也是一个静态方法。例如,请查看此链接以获取有关差异的更多详细信息:https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/

当然,这是一个范围问题,可以通过在全局级别定义变量并在需要的地方初始化来克服。 像——

public class Main {
private static JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame();
button = new JButton("Click Me");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(300,300);
frame.setVisible(true);
Main.changeButtonText();
}
public static void changeButtonText(){
button.setText("I've been clicked");
}
}

你的代码和概念有几点问题,你似乎没有完全掌握。这些概念是this.scopes的。

public void changeButtonText(){
button.setText("I've been clicked");
}

首先,我们需要谈谈作用域。作用域是在{ /* here */ }之间声明的内容,即两个大括号。从作用域内,您可以访问和声明局部和全局对象、变量、成员等

因此,下面是一个使用本地范围的示例(您所做的(:

public void myFunction(){
JButton button = new Jbutton();
}

如果我继续尝试访问范围之外的按钮,它将不知道button是什么。因为它不是在该本地范围内声明的。

public void myFunction(){
JButton button = new Jbutton();
}
public static void main(String[] args) {
button.setText("hello"); //error
// compiler thinks: what's `button` referring to here? its not declared in this scope.
}

全局类范围:

JButton button = new JButton(); //declaring button in global class scope
public void myFunction(){
this.button.setText("hello"); // will access the globally declared variable, object, member called `button`.
//^ notice the usage of `this`. 
//`this` will look outside of the local functions scope for publically declared class-members.
}

要解决您的特定问题,您可以将函数设为静态并传入JButton对象,或者您可以在全局类作用域中声明您的按钮,如下所示:

public class Main {
JButton button = new JButton("Click Me"); //global class scope
public static void main(String[] args) { //local scope of main start {
Main myClass = new Main(); //instantiate class
myClass.changeButtonText(); //access class-member function
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this.button); // this.
frame.setSize(300,300);
frame.setVisible(true);
} // local scope of main ends }
public void changeButtonText(){
this.button.setText("I've been clicked"); //access global class members using `this` keyword
}
}

最新更新