A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© woshihuoye 中级黑马   /  2013-12-29 00:53  /  1218 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Student
{   private int age ;
private static Student s = new Student();
private Student(){}
public static Student getStudent()
{
  return s;
}
public void setAge(int age)
{
  this.age=age;
}
public int getAge()
{
  return age;
}
}
class StudentDemo
{ Student s1=Student.getStudent();
Student s2=Student.getStudent();
s1.setAge(20);
public static void mian()
{
  System.out.println(s2.getAge);
}
}
{:soso_e103:}这段代码为什么编译时:
需要标示符s1.setAge(20);
非法的类型开始需要标示符s1.setAge(20);
搞不懂啊,大家能告诉我吗?

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

11 个回复

倒序浏览
下面给你改好了,我在上面加了注释,你对照自己的看一下都哪错了,细心点哦~

  1. class Student
  2. {   
  3.         private int age ;
  4.         private static Student s = new Student();
  5.         Student(){}
  6.         public static Student getStudent()
  7.         {
  8.           return s;
  9.         }
  10.         public void setAge(int age)
  11.         {
  12.           this.age=age;
  13.         }
  14.         public int getAge()
  15.         {
  16.           return age;
  17.         }
  18. }
  19. class Hello
  20. {
  21.        
  22.         public static void main(String[] args)
  23.         {
  24.                
  25.                 Student s1=Student.getStudent();
  26.                 Student s2=Student.getStudent();
  27.                 s1.setAge(20);
  28.           
  29.           System.out.println(s2.getAge());
  30.         }
  31. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
没加注释,你应该懂得
回复 使用道具 举报
楼主不知道你想问什么  你写的这个是单实例其中的一种 叫做饿汉式
  1. package com.mth.lingxing;

  2. //类一加载就创建对象并初始化
  3. class Student {
  4.         private int age;
  5.         // 第一步:将对象私有化
  6.         private static Student s = new Student();

  7.         private Student() {
  8.         }

  9.         // 第二步:提供一个可以让外面访问的方法(获取对象)
  10.         public static Student getStudent() {
  11.                 System.out.println("创建");
  12.                 return s;
  13.         }

  14.         public void setAge(int age) {
  15.                 this.age = age;
  16.         }

  17.         public int getAge() {
  18.                 return age;
  19.         }
  20. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
主函数缺了点儿东西         public static void main(String[] args)
回复 使用道具 举报
duanyoujiji 发表于 2013-12-29 10:15
主函数缺了点儿东西         public static void main(String[] args)

还有你那个调用getAge()方法没写括号
回复 使用道具 举报
DOOR 发表于 2013-12-29 01:39
下面给你改好了,我在上面加了注释,你对照自己的看一下都哪错了,细心点哦~
...

我们写的没什么区别啊,怎么我的编译不了?
回复 使用道具 举报
woshihuoye 发表于 2013-12-29 10:51
我们写的没什么区别啊,怎么我的编译不了?

public static void main(String[] args)主函数括号里的你没写
s2.getAge调用的方法忘记加括号s2.getAge();
调用代码放进主函数
回复 使用道具 举报
[img]file:///C:/Documents%20and%20Settings/Administrator/Application%20Data/Tencent/Users/836825171/QQ/WinTemp/RichOle/SP21~}DN6%YI98[H93ZY72G.jpg[/img] 按你说的改了,还是这个报错。。。你看驶什么原因呢?
回复 使用道具 举报
duanyoujiji 发表于 2013-12-29 10:17
还有你那个调用getAge()方法没写括号

改过来了,还是报错,你看这个是错在哪了?

SP21~}DN6%YI98[H93ZY72G.jpg (26.31 KB, 下载次数: 16)

SP21~}DN6%YI98[H93ZY72G.jpg
回复 使用道具 举报
woshihuoye 发表于 2013-12-29 14:04
改过来了,还是报错,你看这个是错在哪了?

最后一次回复了哦,你把我贴上去的代码拿去运行,首先要把主函数所在的类型改成你自己的StudentDemo,我是用Hello测试的
回复 使用道具 举报
DOOR 发表于 2013-12-29 15:09
最后一次回复了哦,你把我贴上去的代码拿去运行,首先要把主函数所在的类型改成你自己的StudentDemo,我 ...

o,好吧,谢谢了~~:)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马