实 验 报 告 三
课 程 学 号 专业班级 JAVA语言程序设计 XXXXXX 实验项目 姓 名 继承与接口 XXXXX 成 绩 实验日期 指导教师 2012/10/13 XXXXX 计算机科学与技术09级 一【实验目的】
(1)掌握类和对象的创建方法 (2)理解继承的相关概念 (3)掌握子类对象的创建过程 (4)掌握方法的继承与重写
(5)理解多态的概念与上转型对象 (6)理解抽象类和接口的概念及特点 二【实验内容】
【项目一】方法重写
定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。该程序的模板代码见/附件/Example.java:请将其补充完整并调试运行。
(1)程序代码 class People {
protected double weight,height; public void speakHello() {
System.out.println(\ }
public void averageHeight() {
height=173;
System.out.println(\ }
public void averageWeight() {
weight=70;
System.out.println(\ } }
class ChinaPeople extends People {
//double weight,height;
//重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的 //汉语信息 public void speakHello() {
System.out.println(\你好,你吃了吗?\ }
//重写public void averageHeight()方法,要求输出类似
//“中国人的平均身高:168.78厘米”这样的汉语信息 public void averageHeight() {
height=168.78;
System.out.println(\中国人的平均身高:\ }
//重写public void averageWeight()方法,
//要求输出类似“中国人的平均体重:65公斤”这样的汉语信息 public void averageWeight() {
weight=65;
System.out.println(\中国人的平均体重:\ }
public void chinaGongfu() {
//输出中国武术的信息,例如:\坐如钟,站如松,睡如弓\等 System.out.println(\坐如钟,站如松,睡如弓\ } }
class AmericanPeople extends People {
//重写public void speakHello()方法,要求输出类似 //“How do you do”这样的英语信息。 public void speakHello() {
System.out.println(\ }
//重写public void averageHeight()方法 public void averageHeight() {
height=173;
System.out.println(\
}
//重写public void averageWeight()方法 public void averageWeight() {
weight=70;
System.out.println(\ }
public void americanBoxing() {
//输出拳击的信息,例如,“直拳”、“钩拳”等 System.out.println(\直拳,上勾拳\ } }
class BeijingPeople extends ChinaPeople {
//重写public void speakHello()方法,要求输出类似“您好”这样的汉语信息 public void speakHello() {
System.out.println(\您好啊!!!!!!!!!!!!\ }
//重写public void averageHeight()方法 public void averageHeight() {
height=175;
System.out.println(\北京人的平均身高:\ }
//重写public void averageWeight()方法 public void averageWeight() {
weight=20;
System.out.println(\北京人的平均体重:\ }
public void beijingOpera() {
//输出京剧的信息
System.out.println(\京剧?very good!!\ } }
public class Example {
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)运行结果
【项目二】上转型对象 类Employee为抽象类,具有子类YearWorker,MonthWorker和MonthWorker。YearWorker对象按年领取薪水,MonthWorker按月领取,MonthWorker按周领取。Employee类有一个抽象方法: public abstract earnings();
子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。
在Company类中,使用employee数组作为成员,使用不同的子类对象作为数组元素的上转型对象。该程序的模板代码见/附件/HardWork.java:请将其补充完整并调试运行。
(1)程序代码
abstract class Employee {
public abstract double earnings(); }
class YearWorker extends Employee {
public double earnings() {
return 12000; } }
class MonthWorker extends Employee {
public double earnings() {
return 850; } }
class WeekWorker extends Employee {
public double earnings() {
return 150; } }
class Company {
Employee[] employee; double salaries=0;
Company(Employee[] employee) {
this.employee=employee; }
public double salariesPay()