各位大侠帮我看看这个程序:
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 要在文件中申明?这是申明意思呢???
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 要在文件中申明?这是申明意思呢???
声明为public的那个class的名字要和.java文件名一样
改为
class Counter //18
在一个文件中定义多个类文件,只有包含main函数的可以声明为public的,其余的都声明为默认的吧
改成 class Counter 就可以了
只是main函数在非 public 类中 没法run 只能 debug