申明???

HGC_SanXi 2010-04-22 12:54:32
各位大侠帮我看看这个程序:
public class xx
{
public static void main(String[] args)
{
Counter ob;
ob=new Counter("1");
System.out.println(ob);
ob=new Counter("2");
System.out.println(ob);
ob=new Counter("3");
System.out.println(ob);
ob=new Counter("4");
System.out.println(ob);
System.out.println();
System.out.println("Counter be created:"+Counter.getCount()); //15
}
}
public class Counter //18
{
private String phrase;
private static int count=0; //21
public Counter(String str)
{
phrase=str;
count++; //25
}
public String toString()
{
return phrase;
}
public static int getCount()
{
return count;
}
}
错误为公共类Counter 要在文件中申明?这是申明意思呢???
...全文
186 5 打赏 收藏 转发到动态 举报
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zfq642773391 2010-04-22
  • 打赏
  • 举报
回复
public class Counter //18
改为
class Counter //18
在一个文件中定义多个类文件,只有包含main函数的可以声明为public的,其余的都声明为默认的吧
keeya0416 2010-04-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 hgc_sanxi 的回复:]
各位大侠帮我看看这个程序:
public class xx
{
public static void main(String[] args)
{
Counter ob;
ob=new Counter("1");
System.out.println(ob);
ob=new Counter("2");
System.out.println(ob);
ob=new Counter(……
[/Quote]
一个文件中不能有2个public class的
声明为public的那个class的名字要和.java文件名一样
keeya0416 2010-04-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zfq642773391 的回复:]
public class Counter //18
改为
class Counter //18
在一个文件中定义多个类文件,只有包含main函数的可以声明为public的,其余的都声明为默认的吧
[/Quote]
。。。。和文件同名的才可以声明为 public 的,与含不含 main 没关系吧
只是main函数在非 public 类中 没法run 只能 debug
HGC_SanXi 2010-04-22
  • 打赏
  • 举报
回复
感激涕零啊!!!!
keeya0416 2010-04-22
  • 打赏
  • 举报
回复
public class Counter
改成 class Counter 就可以了
相关推荐

62,568

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告