关于接口的问题,谢谢

yzy19860417 2008-04-01 04:17:28
一段关于接口的程序,小弟不太理解,请大虾教教.
interface Sortable{int Compare(Sortable s);// 定义一个接口} 
class Sort{ // 定义一个排序类,仅有一个静态的方法
public static void SelectSort(Sortable a[ ]){
int i, j, k;
Sortable temp;

for(i=0;i<a.length-1;i++){ // 选择排序
k=i;
for(j=i+1;j<a.length;j++)
if(a[k].Compare(a[j])<0) k=j; 红色的地方不明白,Compare是如何实现的?接口算是一种特殊的类吗?
temp=a[i]; a[i]=a[k]; a[k]=temp;
}
}
}
class Student implements Sortable{ // 定义一个学生类
private int score;

Student(int x){ score=x; }

// 实现接口Sortable中的方法
public int Compare(Sortable s){
Student st=(Student)s; // 类型强制转换
return score-st.score;
}

public String toString( ){ return "score="+score; }
}
class Rectangle implements Sortable{ // 矩形类也实现了接口
private int length,width;

Rectangle(int x,int y){ length=x; width=y; }

int area( ){return length*width;}

public int Compare(Sortable s){ // 实现接口
Rectangle rec=(Rectangle)s; // 类型强制转换
return area( )-rec.area( );
}

public String toString( ){ return "area="+area( ); }
}
public class interfaceTest {
public static void main(String args[ ]) {
Student stud[ ]=new Student[20];
int i; 

for( i=0;i<stud.length;i++)
stud[i]=new Student((int)(Math.random( )*100));
Sort.SelectSort(stud); // 排序
for( i=0;i<stud.length;i++)
System.out.println(stud[i].toString( ));
Rectangle R[ ]=new Rectangle[10];

for( i=0;i<R.length;i++) R[i]=newRectangle(
(int)(Math.random( )*10),
(int)(Math.random( )*10));
Sort.SelectSort( R ); // 排序
for( i=0;i<R.length;i++)
System.out.println(R[i].toString( ));
}
}
...全文
82 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhongxin 2008-04-01
  • 打赏
  • 举报
回复
class Rectangle implements Sortable{
private int length,width;
Rectangle(int x,int y){
length=x; width=y;
}
int area( ){
return length*width;
}
// 实现接口
public int Compare(Sortable s){
Rectangle rec=(Rectangle)s; // 类型强制转换
return area( )-rec.area( );
}

public String toString( ){
return "area="+area( );
}
}
public class InterfaceTest {
public static void main(String args[ ]) {
Student stud[ ]=new Student[20];
int i;

for( i=0;i <stud.length;i++)
stud[i]=new Student((int)(Math.random( )*100));
Sort.SelectSort(stud); // 排序
for( i=0;i <stud.length;i++)
System.out.println(stud[i].toString( ));
Rectangle R[ ]=new Rectangle[10];

for( i=0;i <R.length;i++)
R[i]=new Rectangle((int)(Math.random( )*10),(int)(Math.random( )*10));
Sort.SelectSort( R ); // 排序
for( i=0;i <R.length;i++)
System.out.println(R[i].toString( ));
}
}
chenhongxin 2008-04-01
  • 打赏
  • 举报
回复
class Student implements Sortable{ // 定义一个学生类
private int score;

Student(int x){ score=x; }

// 实现接口Sortable中的方法
public int Compare(Sortable s){
Student st=(Student)s; // 类型强制转换
return score-st.score;
}
约翰羊 2008-04-01
  • 打赏
  • 举报
回复
interface   Sortable{
int Compare(Sortable s);// 定义一个接口
}
class Sort{ // 定义一个排序类,仅有一个静态的方法
public static void SelectSort(Sortable a[ ]){
int i, j, k;
Sortable temp;

for(i=0;i <a.length-1;i++){ // 选择排序
k=i;
for(j=i+1;j <a.length;j++)
if(a[k].Compare(a[j]) <0) k=j;
temp=a[i]; a[i]=a[k]; a[k]=temp;
}
}
}

// 定义一个学生类
class Student implements Sortable{
private int score;

Student(int x){
score=x;
}

// 实现接口Sortable中的方法
public int Compare(Sortable s){
Student st=(Student)s; // 类型强制转换
return score-st.score;
}

public String toString( ){
return "score="+score;
}
}
// 矩形类也实现了接口

class Rectangle implements Sortable{
private int length,width;
Rectangle(int x,int y){
length=x; width=y;
}
int area( ){
return length*width;
}
// 实现接口
public int Compare(Sortable s){
Rectangle rec=(Rectangle)s; // 类型强制转换
return area( )-rec.area( );
}

public String toString( ){
return "area="+area( );
}
}
public class InterfaceTest {
public static void main(String args[ ]) {
Student stud[ ]=new Student[20];
int i;

for( i=0;i <stud.length;i++)
stud[i]=new Student((int)(Math.random( )*100));
Sort.SelectSort(stud); // 排序
for( i=0;i <stud.length;i++)
System.out.println(stud[i].toString( ));
Rectangle R[ ]=new Rectangle[10];

for( i=0;i <R.length;i++)
R[i]=new Rectangle((int)(Math.random( )*10),(int)(Math.random( )*10));
Sort.SelectSort( R ); // 排序
for( i=0;i <R.length;i++)
System.out.println(R[i].toString( ));
}
}

接口只是规定了要有什么方法,参数是什么
具体的实现方法要你自己来写
liu_lm_lm 2008-04-01
  • 打赏
  • 举报
回复
不明白楼主什么意思.
healer_kx 2008-04-01
  • 打赏
  • 举报
回复
class Student implements Sortable{    // 定义一个学生类
private int score;

Student(int x){ score=x; }

// 实现接口Sortable中的方法
public int Compare(Sortable s){
Student st=(Student)s; // 类型强制转换
return score-st.score;
}


这不是实现?

62,623

社区成员

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

试试用AI创作助手写篇文章吧