为什么两个缺省类可以写在一个.java文件下?
下面的程序对着他们同在ScoresManager.java文件,但是当修改class Students为public class Students的时候就错,为什么?类缺省访问控制符:这个类只能被同一个包中的类访问或引用,我想问写在同一个文件下,是什么关系?同包?同文件?package Array;
import java.io.*;
class Students {
private double[] scores;
public Students(double[] s){
scores=new double[s.length];
for(int index=0;index <s.length;index++)scores[index]=s[index];
}
public double[] getScorceList(){
return scores;
}
public double getScore(int sno){
if(sno <1||sno> scores.length)return -1;
return scores[sno-1];
}
public boolean has(double score){
boolean found=false;
for(int index=0;index <scores.length;index++){
if(scores[index]==score) found=true;
}
return found;
}
public int count(double low,double high){
int result=0;
for(int index=0;index <scores.length;index++){
if(scores[index]> =low&&scores[index] <=high) result++;
}
return result;
}
public int countAll(){
return scores.length;
}
public void sort(){
for(int index=0;index <scores.length-1;index++){
for(int ptr=scores.length-1;ptr> index;ptr--){
if(scores[ptr]> scores[ptr-1]){
double temp=scores[ptr];
scores[ptr]=scores[ptr-1];
scores[ptr-1]=temp;
}
}
}
}
}
public class ScoresManager{
public static int inputNumer(){
int num;
try {
System.out.println( "请输入全班学生的人数,不合法的则返回 ");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String inputLine=in.readLine();
num=Integer.valueOf(inputLine).intValue();
} catch (IOException e) {
System.out.println( "学生人数输入错误! ");
return 0;
}
if(num <=0||num> 1000){
System.out.println( "输入的学生人数不合理 ");
return 0;
}
return num;
}
public static void input(double[] scoreList){
int index=0;
while(index <scoreList.length){
System.out.println( "第 "+(index+1)+ "为学生的成绩: ");
try {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String inputLine=in.readLine();
scoreList[index]=Integer.valueOf(inputLine).intValue();
} catch (IOException e) {
System.out.println( "输入成绩不合法 ");
continue;
}
if(scoreList[index] <0||scoreList[index]> 100){
System.out.println( "输入的成绩不在合理范围之内! ");
continue;
}
index=index+1;
}
}
public static void output(double[] scoreList){
for(int index=0;index <scoreList.length;index++){
System.out.println((index+1)+ ". "+scoreList[index]);
}
}
public static void main(String[] args){
int num=inputNumer();
if(num==0) return;
double[] scoreList=new double[num];
System.out.println( "请输入每一位学生的成绩 ");
input(scoreList);
Students students=new Students(scoreList);
System.out.println();
if(students.countAll() <5) System.out.println( "没有第5位同学。 ");
else System.out.println( "第5位同学的成绩为 "+students.getScore(5));
System.out.println();
if(students.has(75)) System.out.println( "发现有学生的成绩位75分 ");
else System.out.println( "找不到分数为75的学生 ");
System.out.println();
System.out.println( "不及格有 "+students.count(0, 59));
System.out.println( "成绩优秀的有 "+students.count(90,100));
System.out.println();
System.out.println( "排序后的学生成绩清单为: ");
students.sort();
output(students.getScorceList());
}
}
[解决办法]
如果一个.java文件中 一个类用public 修饰了
文件的命名就要用这个public 修饰的名字来命名
[解决办法]
任意多个都可以,但是public的就只能一个,并且和文件名相同,编译后会生成相应个数的class文件
[解决办法]
一个java文件中可以定义多个class,但是,只能有一个类名与java文件名相同,而这个类就是public的。