java - "this."代码空白输出,主要功能

标签 java function this

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

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/

相关文章:

C++;使用 lambda 有条件地扩展类中的函数(MWE 的段错误)

javascript - 如何将函数编写为事件处理程序和带参数的可调用函数?

java - Jace::虚拟机关机错误

java - 不能从最终的 GestureDetectorCompat 继承

java - Android MQTT 客户端订阅在 API 级别 26 下无法工作

java - 让 Jackson 从 Spring Controller 返回 JSON 对象

多次调用时 Javascript 函数不起作用

c++ - 如何将参数传递给函数?

c++ - 在 this 关键字上调用函数

javascript - javascript对象