当前位置:编程学习 > JAVA >>

JAVA程序

interface Sortable{ void sort(int type); } class Student{ String name; int mark; Student(String n,int m) { name=n; mark=m; } } class Students implements Sortable{ Student stds[]; Student temp; Students(Student[] stds) { this.stds = stds; } void sort(int type) { if(type==0) { temp=stds[0]; stds[0]=stds[1]; stds[1]=stds[2]; stds[2]=temp; } if(type==1) { temp=stds[0]; stds[0]=stds[1]; stds[1]=temp; } } void print() { for(int i=0;i<=2;i++) { System.out.println("Name:"+stds[i].name+"Score:"+stds[i].mark); } } } public class test{ public static void main(String args[]){ Student[] stds=new Student[3]; stds[0]=new Student("Zhangsan",95); stds[1]=new Student("Lisi",80); stds[2]=new Student("Wangwu",100); Students students=new Students(stds); students.sort(0); students.print(); students.sort(1); students.print(); } } 要求所编写的类保证以下程序的输出为: Name:Lisi Score:80 Name:Wangwu Score:100 Name:Zhangsan Score:95 Name:Lisi Score:80 Name:Zhangsan Score:95 Name:Wangwu Score:100 为什么这个程序编译通不过?
答案:接口中的方法都自动是public 权限,可你实现void sort(int type)没有加修饰符,那么他就自动成为包权限,也就是说系统以为你要降低方法的访问权限,这是JAVA规范里不允许的

在void sort(int type)前面加上public 就可以了

public void sort(int type)

上一个:java小程序
下一个:JAVA2编写问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,