为什么这个代码块不能产生预期的输出?它应该返回:

1个

42

MyThisTest a = 42

代码输出为空白。每当我为“ public static void main(String [] args)”设置大括号时,都会弹出很多红线错误。任何人都知道该如何工作...

public class MyThisTest {
public static void main(String[] args){}
private int a;

public MyThisTest() {
this(42); // calls the other constructor
}

public MyThisTest(int a) {
this.a = a; // assigns the value of the parameter a to the field of the same name
}

public void frobnicate() {
int a = 1;

System.out.println(a); // refers to the local variable a
System.out.println(this.a); // refers to the field a
System.out.println(this); // refers to this entire object
}

public String toString() {
return "MyThisTest a=" + a; // refers to the field a
}
}

最佳答案

您的主要功能是相当错误的。它应该创建一个MyThisTest并在其上调用frobnicate()

就像是

public static void main(String[] args)
{
   MyThisTest myThisTest;
   myThisTest.frobnicate();
}

10-05 23:34