gpt4 book ai didi

java - 在类中使用静态方法时没有输出(其中没有 main 函数的类)

转载 作者:行者123 更新时间:2023-12-01 14:05:36 28 4
gpt4 key购买 nike

这是我的主要类(class):

public class App {
public static void main(String[] args){
Student s1=new Student();

};
};
这是创建的类:
class Student {
public static void f1(){
f2();
}
public static String f2(){

return "hello";
}
public Student(){
f1();
}

}
现在,因为我在主类中创建了一个对象 s1,所以调用了构造函数,它有 f1(),所以调用 f1(),现在 f1() 有 f2(),所以调用 f2(),所以我想必须打印“hello”,但根本不打印输出(不打印任何内容)。谁能解释一下可能是什么原因?

最佳答案

打印 返回 值之间存在差异。
如果你想让它被打印出来,你应该尝试做这样的事情:

class Student {
public static void f1(){
f2();
}
public static void f2(){

System.out.print("hello");
}
public Student(){
f1();
}

}

关于java - 在类中使用静态方法时没有输出(其中没有 main 函数的类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63545084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com