大神帮我看看代码

大神帮我看看代码

package com.imooc.singieton;


public class Earth {

//创建类中私有构造方法

public Earth() {

}

//2、创建该类型的私有静态实例

private static Earth one=new Earth();

//3、创建公有静态方法返回静态实例对象

public static Earth getOne() {

return one;

}


}

package com.imooc.test;


import com.imooc.singieton.Earth;


public class TestTwo {

public static void main(String[] args) {

Earth one=Earth.getOne();

System.out.println("第一个地球创建中。。。。。。");

System.out.println("地球诞生");

Earth two=Earth.getOne();

System.out.println("第二个地球创建中。。。。。。");

Earth three=Earth.getOne();

System.out.println("第三个地球创建中。。。。。。");

System.out.println("问:三个地球是同一个么?");

System.out.println("one");

System.out.println("two");

System.out.println("three");

}


}

http://img1.sycdn.imooc.com//climg/5a817d000001c85005300169.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

5回答
三生树 2018-02-22 10:20:58

两个问题

public Earth() {

}

构造方法修饰符要使用private 但是这里不用不应该也不会报错

System.out.println("one");

System.out.println("two");

System.out.println("three");

输出对象不用加""

但应该也不会报错才对

可能和你环境设置有关系吧 我也不清楚

编译的时候Eclipse有报错吗?

呆呆的小蚂蚁 2018-02-12 21:23:25

标点符号都要英文状态下输入,你好好检查检查。我写的代码能正常运行,不嫌弃的话对着修改修改你的,

public class Test {

    public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("第一个地球创建中。。。。");

Earth earth1=Earth.GetEarth();//类名调用静态方法

System.out.println("第二个地球创建中。。。。");

Earth earth2=Earth.GetEarth();//类名调用静态方法

System.out.println("第三个地球创建中。。。。");

Earth earth3=Earth.GetEarth();//类名调用静态方法

System.out.println("问:三个地球是同一个么?");

System.out.println(earth1+"\n"+earth2+"\n"+earth3);

}

}


public class Earth {

     //定义私有构造方法,并在构造方法中打印输出“地球诞生”

     private Earth(){

         System.out.println("地球诞生");

     }

//定义私有静态类对象并完成实例化

private static Earth earth=new Earth();

//定义公有静态方法返回类内的私有静态对象

public static Earth GetEarth(){

    return  earth;//返回私有静态对象

}

}


  • 提问者 慕神7119415 #1
    谢谢,我照着你的写就可以了,我的也不知道是这么回事,能把你的微信加上,有困难找你吗?或者你加我的1774807864
    2018-02-12 21:43:29
  • 呆呆的小蚂蚁 回复 提问者 慕神7119415 #2
    我也是个小菜鸟,只是大学学的java,出来没从事java,哈哈,女生嘛,那就相互鼓励吧,
    2018-02-12 21:48:54
  • 呆呆的小蚂蚁 回复 提问者 慕神7119415 #3
    你加我微信吧15120001967
    2018-02-12 21:52:46
提问者 慕神7119415 2018-02-12 21:21:38
  • 提问者 慕神7119415 #1
    这个程序一run了,就出现这个标志
    2018-02-12 21:22:42
呆呆的小蚂蚁 2018-02-12 20:04:17

System.out.println("one");

System.out.println("two");

System.out.println("three");

也是不对的

System.out.println(one);

System.out.println(two);

System.out.println(three);


呆呆的小蚂蚁 2018-02-12 20:02:42

//创建类中私有构造方法

private Earth() {

}

你写的是公有构造方法


  • 提问者 慕神7119415 #1
    改了还不能行啊
    2018-02-12 21:16:09
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师