| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 702 人关注过本帖
标题:那位大哥、大姐,能帮我解决关于:“多态”的问题,下面是一段程序,还有错 ...
只看楼主 加入收藏
314104507
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2005-6-16
收藏
 问题点数:0 回复次数:3 
那位大哥、大姐,能帮我解决关于:“多态”的问题,下面是一段程序,还有错误信息!
* 通过这个程序,我们要测试接口的多重实现,并学习对象比较方法的实现 */ import java.util.Arrays;

public class MuilInterfaceTest { public static void main(String[] args) { Student[] staff = new Student[3]; staff[0] = new Student("Tom", "20031020"); staff[1] = new Student("Jack", "20031022"); staff[2] = new Student("Rose", "20021023"); Arrays.sort(staff); for(int i = 0; i < staff.length; i++) { System.out.println((Student)staff[i]); } } } interface Person { String getName(); String getSex(); String getBirthday(); String getAddress(); //void setAddress(String StrAddress); } /* *学生类,包括学生的基本信息,实现了Person与Comparable接口 */ class Student implements Person, Comparable { private String strName = "";//学生姓名 private String strNumber = "";//学号 private String strSex = "";//性别 private String strBirthday = "";//出生年月 private String strSpeciality = "";//专业 private String strAddress = "";//地址

public Student(String name, String number) { strName = name; strNumber = number; } public int compareTo(Object otherObject) { Student other = (Student)otherObject; int otherNumber = Integer.parseInt(other.strNumber); int thisNumber = Integer.parseInt(this.strNumber); if(thisNumber > otherNumber) return 1; else if (thisNumber == otherNumber) return 0; else return -1; }

public String getName() { return strName; }

public String getStudentNumber() { return strNumber; }

public void setStudentSex(String sex) { strSex = sex; }

public String getSex() { return strSex; }

public String getBirthday() { return strBirthday; }

public void setStudentBirthday(String birthday) { strBirthday = birthday; }

public String getStudentSpeciality() { return strSpeciality; }

public void setStudentSpeciality(String speciality) { strSpeciality = speciality; }

public String getAddress() { return strAddress; }

public void setAddress(String address) { strAddress = address; }

public String toString() { String information = "学生姓名=" + strName + ", 学号=" + strNumber; if( !strSex.equals("") ) information += ", 性别=" + strSex; if( !strBirthday.equals("")) information += ", 出生年月=" + strBirthday; if( !strSpeciality.equals("") ) information += ", 专业=" + strSpeciality; if( !strAddress.equals("") ) information += ", 籍贯=" + strAddress; return information; } }

这个程序是关于“多态”的,这个程序编译时能通过,但是在运行时却出现了以下的错误,请大哥、大姐们多多指教: Exception in thread "main" java.lang.ClassCastException at java.util.Arrays.mergeSort(Arrays.java:1152) at java.util.Arrays.sort(Arrays.java:1079) at MuilInterfaceTest.main(MuilInterfaceTest.java:16)

搜索更多相关主题的帖子: 多态 大姐 
2005-06-16 20:14
au
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2005-6-9
收藏
得分:0 
你这个程序我用eclipse编译运行成功,以下是运行结果:
学生姓名=Rose, 学号=20021023
学生姓名=Tom, 学号=20031020
学生姓名=Jack, 学号=20031022

2005-06-16 20:56
tempnetbar
Rank: 2
等 级:新手上路
威 望:4
帖 子:582
专家分:4
注 册:2004-5-5
收藏
得分:0 
同楼上,JDK1.5+Editplus,通过并显示结果,代码没有问题

相信勤能补拙! 喜欢用好用的就永远学不到有用的。
2005-06-17 07:26
314104507
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2005-6-16
收藏
得分:0 
非常感谢你能帮小弟解决了这个问题,但是小弟还不知道eclipse怎么用,能告诉小弟吗?小弟万分感激!
2005-06-17 08:18
快速回复:那位大哥、大姐,能帮我解决关于:“多态”的问题,下面是一段程序,还 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011856 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved