Java &引用;这一点;代码空白输出,主功能
为什么这段代码不能产生它应该产生的输出?它应返回: 一, 42 MyThisTest a=42 代码输出为空。每当我为“publicstaticvoidmain(String[]args)”设置花括号时,就会弹出很多红线错误。任何人都知道怎么做Java &引用;这一点;代码空白输出,主功能,java,function,this,Java,Function,This,为什么这段代码不能产生它应该产生的输出?它应返回: 一, 42 MyThisTest a=42 代码输出为空。每当我为“publicstaticvoidmain(String[]args)”设置花括号时,就会弹出很多红线错误。任何人都知道怎么做 public class MyThisTest { public static void main(String[] args){} private int a; public MyThisTest() { this(42); // calls the
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
并对其调用frobnite()
差不多
public static void main(String[] args)
{
MyThisTest myThisTest;
myThisTest.frobnicate();
}
您错过了主功能中的任何操作:
public static void main(String[] args){}
应该是:
public static void main(String[] args)
{
MyThisTest thisTest = new MyThisTest();
thisTest.frobnicate();
}
主函数是启动程序时调用的函数。
如果你在那里什么都不做,什么也不会发生。因此,原始代码中的输出保持空白。Main方法是java程序的起点,您需要在那里初始化对象。在初始化对象之前,无法调用已定义的构造函数。Frobnite方法也是对象级方法,只能在实例上调用。您应该在main方法中完成所有这些操作(正如其他人已经解释过的)您应该添加语言标记。