为什么这段代码不能产生它应该产生的输出?它应该返回:
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();
}
关于java - "this."代码空白输出,主要功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349362/