实验十 类继承与接口 二

上传人:第*** 文档编号:34083312 上传时间:2018-02-20 格式:DOC 页数:7 大小:60.50KB
返回 下载 相关 举报
实验十  类继承与接口 二_第1页
第1页 / 共7页
实验十  类继承与接口 二_第2页
第2页 / 共7页
实验十  类继承与接口 二_第3页
第3页 / 共7页
实验十  类继承与接口 二_第4页
第4页 / 共7页
实验十  类继承与接口 二_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《实验十 类继承与接口 二》由会员分享,可在线阅读,更多相关《实验十 类继承与接口 二(7页珍藏版)》请在金锄头文库上搜索。

1、实验十 类继承与接口(二)课程名称: 面向对象程序设计实验 实验项目名称: 实验十 类继承与接口 (二) 学生姓名: 葛添翼 专业: 计算 0903 学号: 30901183 实验地点: 实验日期: 年 月 【实验目的】1掌握 Java 类的继承特点,包括子类声明、对象创建、成员变量的继承与隐藏、成员方法的继承与重写【实验内容】10-1 程序阅读分析题。要求(1)有一个 abstract 类,类名为 Employee。Employee 的子类有YearWorker,MonthWorker 和 WeekWorker。YearWorker 对象按年领取薪水,MonthWorker按月领取薪水,We

2、ekWorker 按周领取薪水。Employee 类有一个 abstract 方法,子类必须重写父类的 earings()方法,给出各自领取报酬的具体方式。(2 )有一个 Company 类,该类用 employee 数组作为成员, employee 数组的单元可以是 YearWorker 对象的上转型对象,MonthWorker 对象 的上转型对象或者 WeekWorker 对象的上转型对象。源程序:(请将源程序中补充的空缺代码处以下划线加粗显示):abstract class Employeepublic abstract double earnings();class YearWorke

3、r extends Employee/重写 earnings()方法代码 1:public double earnings()return 10000;class MonthWorker extends Employee/重写 earnings()方法代码 2:public double earnings()return 1000;class WeekWorker extends Employee/重写 earnings()方法代码 3:public double earnings()return 100;class CompanyEmployee employee;double salari

4、es=0;Company(Employee employee)this.employee=employee;public double salariesPay()salaries=0;/计算 salaries代码 4: int i;for(i=0;iemployee.length;i+)salaries+=employeei.earnings();return salaries;return salaries;public class HardWorkpublic static void main(String args)Employee employee=new Employee20;for

5、(int i=0;iemployee.length;i+)if(i%3=0)employeei=new WeekWorker();else if(i%3=1)employeei=new MonthWorker();else if(i%3=2)employeei=new YearWorker();Company company=new Company(employee);System.out.println(公司年工资总额:+company.salariesPay();10-2.检查下面代码是否有错,如果有错,写明错误原因,并修正错误。1. class Aaaint i;Aaa()i=-1; A

6、aa(int a) i = a;Aaa(double d)Aaa(int)d);是否出错:出错原因(如果出错):出错没有为 A.Aaa 定义方法 Aaa(int)2. class Aaaint i;Aaa()i=-1; Aaa(int a) i = a;Aaa(double a)int b=(int)a;this(b); 是否出错:出错原因(如果出错):正确3. class Aaaint i;Aaa(int a) i = a;class Bbb extends AaaBbb() 是否出错: 出错原因(如果出错):正确4. class Aaaprivate int i;protected int

7、 j;class Bbb extends AaaBbb()i = 10;j = 99;是否出错:出错原因(如果出错):错误超类中 Private 类型的变量子类不能继承5. 编译下面程序,程序是否出错class Aint i =100;class B extends Aint i =10000;public static void main(String args)System.out.println(this.i);System.out.println(super.i);a) 是否出错:错误b) 出错原因(如果出错):不能在静态方法中使用 this 和 super(2)如果将上述类 B 的

8、int i =10000 改为 static int i =10000;类 A 的 int i =100 改为 static int i =100;程序是否出错?出错原因(如果出错):出错。同上(3)如果将类 B 的 main 方法改为public static void main(String args)B b =new B();System.out.println(b.i); System.out.println(A)b.i); 程序是否出错? 出错原因(如果出错):出错不能强制将 int 类型转换为 A 类型。如果没出错, 给出结果:上面的 System.out.println(b.i)

9、的含义: System.out.println(A)b.i) 的含义 : 6. class Aaaint i;public void Aaa(int a) i=a;public static void main(String args)Aaa a = new Aaa(10); 是否出错:出错。出错原因(如果出错):不能使用 PUBLIC void 修饰7.class Aaapublic static void main(String args)int a=new int5;for( int i=0;ia.length;i+)for(int j=0;iai.length;j+)aij= (int

10、)(100*Math.random(); System.out.println(aij); 是否出错:出错原因(如果出错):出错for(int j=0;iai.length;j+)这句出现无限循环。8.class Aint abc(int i)return 1; /方法 1int abc(int j)return 2; /方法 2int abc(int i,int j)return 3; /方法 3void abc(int i) /方法 4是否有错: 没有上面类 A 中定义的四个方法是否都是方法的重载,有没有重复定义的方法,如果有,那么哪几个方法是重复定义的?是的前面 3 个是重复定义的9.

11、class Avoid show()System.out.println(A 类的 show();class B extends Aint show()System.out.println(B 类的 show();public static void main(String args)B b = new B();b.show();上面程序中的方法覆盖是否出错:出错B 中的方法与 A 中的方法返回类型不一致出错原因(如果出错):如果将类 B 中的方法 show()改为:int show(int b)System.out.println(B 类的 show();return b;程序是否出错? 正确了实验报告要求将编程题源程序、运行结果,以及实验中遇到的问题和解决问题的方法,写在实验报告上。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号