地球诞生怎么出现在第一个调用后面的,不知道为什么
public class Earth { //定义私有构造方法,并在构造方法中打印输出“地球诞生” private Earth(){ System.out.println("地球诞生"); } //定义私有静态类对象并完成实例化 private static Earth earth=new Earth(); //定义公有静态方法返回类内的私有静态对象 public static Earth getEarth(){ return earth; } } public class Test { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("第一个地球创建中。。。。"); Earth one=Earth.getEarth(); System.out.println("第二个地球创建中。。。。"); Earth two=Earth.getEarth(); System.out.println("第三个地球创建中。。。。"); Earth three=Earth.getEarth(); System.out.println("问:三个地球是同一个么?"); System.out.println(one); System.out.println(two); System.out.println(three); } }
55
收起
正在回答 回答被采纳积分+1
2回答
Tender10
2017-07-12 09:44:15
public class Earth { //定义私有构造方法,并在构造方法中打印输出“地球诞生” private Earth(){ System.out.println("地球诞生"); } //定义私有静态类对象并完成实例化 private static Earth earth=new Earth(); //定义公有静态方法返回类内的私有静态对象 public static Earth getEarth(){ return earth; } }
上述代码中,因为在类中已经完成了Earth类对象的初始化new Earth(),已经将"地球诞生"赋值给了Earth
,所以在Test类中Earth创建一个对象one,并且调用getEarth()方法去获取值,而获取到的值就是"地球诞生"。
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星