第九次实验
实验一
1、编写一个 Java 程序 , 程序中有一个父类 Telephone,Telephone 类中包含有电话品牌、电话号码、通话时间、费率等属性 , 以及计算话费和显示信息等方法。另外 , 程序中还有另一个类叫作 Mobilephone, 它是 Telephone 的子类 , 除了具有 Telephone 类的属性外 , 它还有自己的属性如网络类型、被叫时间 , 同时它有自己的计算话费和显示信息的方法。 最后程序中应包含一个主类来使用上述两个类并显示它们的信息。 2、代码
class Telephone { String brand, number; double dialledTime; double rate;
Telephone(String b, String n) { brand = b; number = n; }
String getBrand( ) { return brand; }
String getNumber( ) { return number; }
double getrate( ) {
return rate;//代码1 //返回费率值 }
double getDialledTime( ) { return dialledTime; }
void setBrand(String b) { brand = b; }
void setNumber(String n) {
number=n;//代码2 // 设置电话号码 }
void setRate(double r) { rate = r; }
void setDialledTime(double d) { dialledTime = d; }
double callCost( ) {
return dialledTime * rate; }
void display( ) {
System.out.println(\电话品牌: \电话号码: \System.out.println(\通话时间: \费率: \System.out.println(\话费: \} }
class Mobilephone extends Telephone { String network;
double receivedTime;
Mobilephone(String b, String num, String net) { super(b, num); network = net; }
String getNetwork( ) { return network; }
double getReceivedTime( ) { return receivedTime; }
void setNetwork(String n) { network = n; }
void setReceivedTime(double d) {
dialledTime=d;//代码3 //设置被叫时间 }
double callCost( ) {
return (dialledTime + 0.5*receivedTime) * rate; }
void display( ) {
System.out.println(\电话品牌: \电话号码: \+\网络: \
System.out.println(\主叫时间: \被叫时间: \receivedTime +\费率: \
System.out.println(\话费: \} }
public class Inheritance {
public static void main(String[] args) { Telephone tel;
Mobilephone mobile;
tel = new Telephone(\
mobile = new Mobilephone(\tel.setRate(0.2);
tel.setDialledTime(150); mobile.setRate(0.4);
mobile.setDialledTime(80);//代码4 //设置主叫时间为80 mobile.setReceivedTime(120); tel.display( );
System.out.println( );
mobile.display();//代码5 //显示移动电话的信息 } }
3、结果
4、练习
(1) 在 Mobilephone 类的 display() 方法中有如下语句 : System.out.println(\话费 :\
试问这里调用的是子类自己的 callcost() 还是其父类的 callcost(); 如果想要在此处调用 父类的 callcost() 应如何处理。 子类 super.callCost()
(2) 子类 Mobilephone 的构造函数中有语句 super(b,num): 请指出该语句的作用。 然后请将该语句删除掉 , 编译运行程序 , 看看是否会出现编译错误 , 并分析原因。
调用父类的构造方法 会出错 在调用子类构造方法时总是先调用父类的构造方法
事实上 , 如果在子类的构造函数中不显式地使用 super 来调用父类的构造函数的话 ,那么 系统将自动调用父类的无参或默认的构造函数。因此为了纠正这个错误 , 可试着在Telephone 类中加入一个无参的构造函数。
Telephone() { brand = null; number = null; }
(3) 要调用父类的构造函数可以用 super 关键字 , 而要调用本类自己的构造函数可以用 this 关键字 , 请试着在 Telephone 类中加入一个构造函数如下 : Telephone (String b ,String n,double r){ this(b,n); rate =r; }
然后在主程序中用这个构造函数来创建一个 Telephone 对象 , 看看它能否起作用。 能起作用
实验二
1、请设计 3 个类 , 分别是学生类 Student, 本科生类 Undergaduate, 研究生类 Postgraduate,其中 Student 类是一个抽象类 , 它包含一些基本的学生信息如姓名、所学课程、课程成绩等 , 而Undergraduate 类和 Postgraduate 都是 Student 类的子类 , 它们之间的主要差别是计算课程成绩等级的方法有所不同 , 研究生的标准要比本科生的标准高一些 , 如表 1-2 所示。
表 1-2 课程成绩等级 本科生标准 80--100 70--80 60--70 50--60 50 以下 优秀 良好 一般 及格 不及格 80--90 70--80 60--70 60 以下 研究生标准 90--100 优秀 良好 一般 及格 不及格 假设某班级里既有本科生也有研究生 , 请编写程序统计出全班学生的成绩等级并显示出来。此题关键是设计一个学生数组 , 既能存放本科生对象 , 又能存放研究生对象。 2、代码
abstract class Student {
final static int CourseNo = 3; String name; String type;
int[] courses;
String courseGrade;
public Student(String name) { this.name = name;
courses = new int[CourseNo];
courseGrade=\代码1 //初始化courseGrade为空串 }
public abstract void calculateGrade();
public String getName( ) { return name; }
public String getType( ) {
return type;//代码2 //返回学生类型 }
public String getCourseGrade( ) { return courseGrade; }
public int getCourseScore(int courseNumber) { return courses[courseNumber]; }
public void setName(String name) { this.name = name; }
public void setType(String type) { this.type = type; }
public void setCourseScore(int courseNumber, int courseScore) {
courses[courseNumber]=courseScore;//代码3 //按课程索引号设置课程成绩 } }
class Undergraduate extends Student { public Undergraduate(String name ) { super(name); type = \本科生\ }
public void calculateGrade() {