为什么编译老是出现这种提示.请大家帮帮忙.
F:\java> javac Date3_ex.java
Date3_ex.java:1: class Date3 is public, should be declared in a file named Date3
.java
public class Date3 //类的封装
^
1 error
源程序如下:
class Date3 //类的封装
{
private int year,month,day; //成员变量,私有的
public Date3(int y,int m,int d)
{
year = y;
month = (((m> =1) & (m <=12)) ? m : 1);
day = (((d> =1) & (d <=31)) ? d : 1);
}
public int getyear() //成员方法,公有的
{
return year;
}
public boolean isleapyear()
{
return (year%400==0) | (year%100!=0) & (year%4==0);
}
public void print()
{
System.out.println( "date is "+year+ '- '+month+ '- '+day);
}
}
public class Date3_ex //其他类
{
public static void main(String args[])
{
Date3 a = new Date3(2002,6,28) ;
a.print();
System.out.println(a.getyear()+ " is a leap year, "+a.isleapyear());
}
}
------解决方案--------------------public的class
class名要和.java的文件名一样
------解决方案--------------------错误信息说得很清楚,为什么还要问呢?
------解决方案--------------------建议你将class Date3 的类名字改一下
从F:\java> javac Date3_ex.java和public class Date3_ex