java 继承与接口 实验三_第1页
java 继承与接口 实验三_第2页
java 继承与接口 实验三_第3页
java 继承与接口 实验三_第4页
java 继承与接口 实验三_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

信息与计算科学专业实验报告 课程名称Java 课程设计总实验学时 16 第 3 次共 6 次 实验项目名称继承与接口本次实验学时数 3 实验类 型 验证 日期20 12 年 3 月 6 日星期 二 年级 学生姓名学号课任教师 1 1 实验目的实验目的 巩固如下概念 子类的继承性 子类对象的创建过程 成员变量的继承与隐藏 方法的继 承与重写 掌握上转型对象的使用 掌握接口回调技术 2 2 实验要求实验要求 实验前 应事先熟悉相关知识点 拟出相应的实验操作步骤 明确实验目的和要求 实验 过程中 服从实验指导教师安排 遵守实验室的各项规章制度 爱护实验仪器设备 实验操作 完成后 认真书写实验报告 总结实验经验 分析实验过程中出现的问题 3 3 实验内容实验内容 1 继承 编写一个 Java 应用程序 除主类外 该程序中还有 4 个类 People ChinaPeople AmericanPeople 和 BeijingPeople 类 要求如下 People 类有访问权限是 protected 的 double 型成员变量 height 和 weight 以及 public void speakHello public void averageHeight public void averageWeight 方法 ChinaPeople 类是 People 的子类 新增了 public void chinaGongfu 方法 要求 ChinaPeople 重写父类的 public void speakHello public void averageHeight public void averageWeight 方法 AmericanPeople 类是 People 的子类 新增 public void americanBoxing 方法 要求 AmericanPeople 重写父类的 public void speakHello public void averageHeight public void averageWeight 方法 BeijingPeople 类是 ChinaPeople 的子类 新增 public void beijingOpera 方法 要求 BeijingPeople 重写父类的 public void speakHello public void averageHeight public void averageWeight 方法 请按模板要求 将 代 码 替换为 Java 程序代码 2 上转型对象 编写一个 Java 应用程序 要求有一个 abstract 类 类名为 Employee Employee 的子类 有 YearWorker MonthWorker 和 WeekWorker YearWorker 对象按年领取薪水 MonthWorker 按月领取薪水 WeekWorker 按周领取薪水 Employee 类有一个 abstract 方法 public abstract double earnings 子类必须重写父类的 earnings 方法 给出各自领取报酬的具体方式 有一个 Company 类 该类用 Employee 数组作为成员 Employee 数组的元素可以是 YearWorker 对象的上转型对象 MonthWorker 对象的上转型对象或 WeekWorker 对象的上转 型对象 程序能输出 Company 对象一年需要支付的薪水总额 3 接口回调 卡车要装载一批货物 货物有 3 种商品 电视 计算机和洗衣机 需要计算出大货车和小 货车各自所装载的 3 中货物的总重量 编写一个 Java 应用程序 要求有一个 ComputeWeight 接口 该接口中有一个方法 public double computeWeight 有 3 个实现该接口的类 Television Computer 和 WashMachine 这 3 个类通过实现接口 ComputeWeight 给出自重 有 一个 Car 类 该类用 ComputeWeight 接口类型的数组作为成员 那么该数组的元素就可以存 放 Television 对象的引用 Computer 对象的引用或 WashMachine 对象的引用 程序能输出 Car 对象所装载的货物的总重量 4 4 实验步骤 实施过程 关键代码 实验结果及分析说明等 实验步骤 实施过程 关键代码 实验结果及分析说明等 1 1 代码 代码 class People protected double weight height public void speakHello System out println yayawawa public void averageHeight height 173 System out println average height height public void averageWeight weight 70 System out println average weight weight class ChinaPeople extends People public void speakHello System out println 你好 睡了吗 代码 1 重写 public void speakHello 方法 public void averageHeight System out println 中国人的平均身高 168 78 厘米 代码 2 重写 public void averageHeight 方法 public void averageWeight System out println 中国人的平均体重 65 公斤 代码 3 重写 public void averageWeight 方法 public void chinaGongfu System out println 坐如钟 站如松 睡如弓 代码 4 输出中国武术的信息 class AmericanPeople extends People public void speakHello System out println How do you do 代码 5 重写 public void speakHello 方法 public void averageHeight System out println 美国人平均身高为 175 厘米 代码 6 重写 public void averageHeight 方法 public void averageWeight System out println 美国人平均体重为 75 公斤 代码 7 重写 public void averageWeight 方法 public void americanBoxing System out println 直拳 钩拳 代码 8 输出拳击的信息 class BeijingPeople extends ChinaPeople public void speakHello System out println 你好 代码 9 重写 public void speakHello 方法 public void averageHeight System out println 北京人平均身高为 170 厘米 代码 10 重写 public void averageHeight 方法 public void averageWeight System out println 北京人平均体重为 65 公斤 代码 11 重写 public void averageWeight 方法 public void beijingOpera System out println 京剧的形成大约有 150 年左右 代码 12 输出京剧的信 息 public class Example3 1 public static void main String args ChinaPeople chinaPeople new ChinaPeople AmericanPeople americanPeople new AmericanPeople BeijingPeople beijingPeople new BeijingPeople chinaPeople speakHello americanPeople speakHello beijingPeople speakHello chinaPeople averageHeight americanPeople averageHeight beijingPeople averageHeight chinaPeople averageWeight americanPeople averageWeight beijingPeople averageWeight chinaPeople chinaGongfu americanPeople americanBoxing beijingPeople beijingOpera beijingPeople chinaGongfu 实验结果 实验结果 2 2 代码 代码 abstract class Employee double salary public abstract double earnings 返回年收入 class YearWorker extends Employee public double earnings return 4000 重写 earnings 方法 class MonthWorker extends Employee public double earnings return 3000 重写 earnings 方法 class WeekWorker extends Employee public double earnings return 2000 重写 earnings 方法 class Company Employee employee double salaries 0 Company Employee employee this employee employee public double salariesPay salaries 0 for int i 0 i employee length i salaries salaries employee i earnings 计算 salaries return salaries public class HardWork public static void main String args Employee employee new Employee 20 for int i 0 i employee length i if i 3 0 employee i new WeekWorker else if i 3 1 employee i new MonthWorker else if i 3 2 employee i new YearWorker Company company new Company employee System out println 公司年工资总额 company salariesPay 实验结果 实验结果 3 3 代码 代码 interface ComputerWeight public double computerWeight class Television implements ComputerWeight public double computerWeight return 155 class Computer implements ComputerWeight public double computerWeight return 105 class WashMachine implements ComputerWeight public double computerWeight return 80 class Car ComputerWeight goods double totalWeights 0 Car ComputerWeight goods this goods goods public double getTotalWeights totalWeights 0 for int i 0 i goods length i totalWeights goods i computerWeight return totalWeights public class Road public static void main String args ComputerWeight goodsOne new ComputerWeight 50 goodsTwo new ComputerWeight 22 for int i 0 i goodsOne length i if i 3 0 goodsOne i new Television else if i 3 1 goodsOne i new Computer else if i 3 2 goodsOne i ne

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论