实验5 继承与接口.doc_第1页
实验5 继承与接口.doc_第2页
实验5 继承与接口.doc_第3页
实验5 继承与接口.doc_第4页
实验5 继承与接口.doc_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

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

文档简介

实验5 继承与接口实验目的1、掌握java 继承中父类及其子类的定义方法。2、掌握子类重写父类同名方法的方法。3、掌握接口的用法。(1) 学习如何定义接口 ;(2) 掌握接口的实现方式 ;(3) 使用实现了接口的类 ; (4) 理解接口与抽象类的区别。实验要求1、 复习理论教学中所学的内容。2、 认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。3、 认真总结实验并书写实验报告。实验课时 2学时实验教学方式 学生上机实验,教师随堂指导。实验内容1、 类的继承性练习【新类可从现有的类中产生,并保留现有类的成员变量和方法并可根据需要对它们加以修改。新类还可添加新的变量和方法。这种现象就称为类的继承。当建立一个新类时,不必写出全部成员变量和成员方法。只要简单地声明这个类是从一个已定义的类继承下来的,就可以引用被继承类的全部成员。被继承的类称为父类或超类(superclass),这个新类称为子类。Java 提供了一个庞大的类库让开发人员继承和使用。设计这些类是出于公用的目的,因此,很少有某个类恰恰满足你的需要。你必须设计自己的能处理实际问题的类,如果你设计的这个类仅仅实现了继承,则和父类毫无两样。所以,通常要对子类进行扩展,即添加新的属性和方法。这使得子类要比父类大,但更具特殊性,代表着一组更具体的对象。继承的意义就在于此。】【】中的读完删掉不作为实验报告的一部分(一)创建将被继承的类(1) 程序源代码如下。public class EXP3_7protected String xm; /姓名,具有保护修饰符的成员变量protected int xh;/学号void setdata(String xm,int xh) /设置数据的方法此处添加所需代码public void print() /输出数据的方法System.out.println(xm+, +xh);(2) 编译源程序。public class EXP3_7 protected String xm; /姓名,具有保护修饰符的成员变量protected int xh;/学号void setdata(String xm,int xh) /设置数据的方法this.xh=xh;this.xm=xm;public void print() /输出数据的方法System.out.println(xm+, +xh);(二)创建将被继承的类(1) 程序功能:通过EXP3_7类产生子类EXP3_8,其不仅具有父类的成员变量xm(姓名)、xh(学号),还定义了新成员变量xy(学院)、bj(bj)。在程序中调用了父类的print 方法,同时可以看出子类也具有该方法。(2) 程序源代码如下。class EXP3_8 extends EXP3_7此处定义新的成员变量,均为protected类型public static void main(String args)EXP3_7 p1 = new EXP3_7();p1.setdata(李四,12321) ;p1.print();EXP3_8 s1 = new EXP3_8() ;s1.setdata(张三,12345); /调用父类的成员方法s1.xy=山西大学计算机学院; /访问本类的成员变量s1.bj=2008级计算机科学与技术; /访问本类的成员变量s1.print();System.out.print(s1.xm+, +s1.xy+, +s1.bj);(1) 编译并运行,结果如图3.7所示。class EXP3_8 extends EXP3_7String xy;String bj;public static void main(String args)EXP3_7 p1 = new EXP3_7();p1.setdata(李四,12321) ;p1.print();EXP3_8 s1 = new EXP3_8() ;s1.setdata(张三,12345); /调用父类的成员方法s1.xy=山西大学计算机学院; /访问本类的成员变量s1.bj=2008级计算机科学与技术; /访问本类的成员变量s1.print();System.out.print(s1.xm+, +s1.xy+, +s1.bj);李四, 12321张三, 12345张三, 山西大学计算机学院, 2008级计算机科学与技术运行结果贴图图 3.7(三)了解成员方法的覆盖方式【通过继承子类可以继承父类中所有可以被子类访问的成员方法,但如果子类的方法与父类方法同名,则不能继承,此时称子类的方法覆盖了父类的方法,简称为方法覆盖(override)。方法覆盖为子类提供了修改父类成员方法的能力。例如,子类可以修改层层继承下来的Object 根类的toString 方法,让它输出一些更有用的信息。下面的程序显示了在子类Circle 中添加toString 方法,用来返回圆半径和圆面积信息。】【】中的读完删掉不作为实验报告的一部分(1) 编写覆盖Object 类toString方法的程序文件EXP3_9.java,源代码如下。class Circle private int radius;定义一个参数的构造方法对于私有成员radius进行get和set(定义getRadius()和setRadius() 定义求面积方法area()public String toString() return 圆半径:+getRadius()+ 圆面积:+area();public class EXP3_9public static void main(String args) Circle c=new Circle(10);System.out.println(n+c.toString();(2) 编译并运行,结果如图3.8所示。class Circle private int radius;public Circle(int radius) super();this.radius = radius;public Circle() super();public int getRadius() return radius;public void setRadius(int radius) this.radius = radius;double area()return Math.PI*radius*radius;public String toString() return 圆半径:+getRadius()+ 圆面积:+area();class EXP3 extends Circlepublic static void main(String args) Circle c=new Circle(10);System.out.println(n+c.toString();圆半径:10 圆面积:314.1592653589793(3) 试着以Point类为例,尝试为Object类的clone()和equals()方法进行覆盖,Point类包含私有成员x,y,构造方法1(包含两个参数a,b),构造方法2(参数为Point p),clone方法,equals方法,toString方法。用TestPoint类进行测试。运行结果贴图图 3.8class Objectpublic String clone()return this is clone ;public String equals()return this is equals;public class Point extends Object private double x;private double y;public Point(double a, double b) super();this.x = a;this.y = b;public Point(Point p) public String clone()return this is a clone kkk;public String equals()return this is a equals kkk;public String toString() return Point x= + x + , y= + y + ;public class TestPoint public static void main (String args)Point s=new Point(2,3);System.out.println(s.clone();System.out.println(s.equals();this is a clone kkkthis is a equals kkk(四)this、super和super()的使用(1) 程序功能:程序功能:说明this、super 和super()的用法。程序首先定义Point(点)类,然后创建点的子类Line(线)。最后通过LX3_10 类输出线段的长度。程序中通过super(a,b)调用父类Point 的构造方法为父类的x 和y 赋值。在子类Line 的setLine方法中,因为参数名和成员变量名相同,为给成员变量赋值,使用this 引用,告诉编译器是为当前类的成员变量赋值。在length 和toString 方法中使用父类成员变量时,使用super 引用,告诉编译器使用的是父类的成员变量。(2) 程序源代码如下。class Point protected int x, y;Point(int a, int b) setPoint(a, b);public void setPoint(int a, int b) x=a;y=b;class Line extends Point protected int x, y;Line(int a, int b) super(a, b);setLine(a, b);public void setLine(int x, int y) this.x=x+x;this.y=y+y;public double length() int x1=super.x, y1=super.y, x2=this.x, y2=this.y;return Math.sqrt(x2-x1) * (x2-x1) + (y2-y1) * (y2-y1);public String toString() return 直线端点: + super.x + , + super.y + +x + , + y + 直线长度: + this.length();public class EXP3_10public static void main(String args) Line line=new Line(50, 50);System.out.println(n+line.toString();(3) 编译并运行,结果如图3.9。运行结果贴图class Point protected int x, y;Point(int a, int b) setPoint(a, b);public void setPoint(int a, int b) x=a;y=b;class Line extends Point protected int x, y;Line(int a, int b) super(a, b);setLine(a, b);public void setLine(int x, int y) this.x=x+x;this.y=y+y;public double length() int x1=super.x, y1=super.y, x2=this.x, y2=this.y;return Math.sqrt(x2-x1) * (x2-x1) + (y2-y1) * (y2-y1);public String toString() return 直线端点: + super.x + , + super.y + +x + , + y + 直线长度: + this.length();public class EXP3_10 public static void main(String args) Line line=new Line(50, 50);System.out.println(n+line.toString();直线端点:50,50 100,100 直线长度:70.71067811865476(五)1、定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。该程序的模板代码如下:请将其补充完整并调试运行。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 【代码1】 /重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的 /汉语信息【代码2】 /重写public void averageHeight()方法,要求输出类似 /“中国人的平均身高:168.78厘米”这样的汉语信息【代码3】 /重写public void averageWeight()方法, /要求输出类似“中国人的平均体重:65公斤”这样的汉语信息 public void chinaGongfu() 【代码4】/输出中国武术的信息,例如:坐如钟,站如松,睡如弓等 class AmericanPeople extends People【代码5】 /重写public void speakHello()方法,要求输出类似 /“How do you do”这样的英语信息。【代码6】 /重写public void averageHeight()方法 【代码7】 /重写public void averageWeight()方法 public void americanBoxing() 【代码8】/输出拳击的信息,例如,“直拳”、“钩拳”等 class BeijingPeople extends ChinaPeople 【代码9】 /重写public void speakHello()方法,要求输出类似“您好”这样的汉语信息 【代码10】 /重写public void averageHeight()方法 【代码11】 /重写public void averageWeight()方法 public void beijingOpera() 【代码12】/输出京剧的信息 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(); package chengxu;class Peopleprotected 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(你好,吃了吗?); public void averageHeight() height=168.78;System.out.println(中国人的平均身高:+height+ 厘米);public void averageWeight()weight=70;System.out.println(中国人的平均体重:+weight+公斤);public void chinaGongfu()System.out.println(坐如钟,站如松,睡如弓);class AmericanPeople extends Peoplepublic void speakHello()System.out.println(how do you do); public void averageHeight() height=179;System.out.println(average height:+height+cm);public void averageWeight()weight=90;System.out.println(average weight:+weight+kilogram );public void americanBoxing()System.out.println(直拳,勾拳);class BeijingPeople extends ChinaPeople public void speakHello()System.out.println(您好); public void averageHeight() height=168;System.out.println(average height:+height+厘米);public void averageWeight()weight=75;System.out.println(average weight:+weight+公斤);public void beijingOpera() System.out.println(京剧);package chengxu;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(); 你好,吃了吗?how do you do您好中国人的平均身高:168.78厘米average height:179.0cmaverage height:168.0厘米中国人的平均体重:70.0公斤average weight:90.0kilogram average weight:75.0公斤坐如钟,站如松,睡如弓直拳,勾拳京剧坐如钟,站如松,睡如弓2、读懂下面模板代码,按要求补充程序并调试运行。掌握抽象类的定义及其实现方法,学习上转型对象的运用方法。abstract class Employee public abstract double earnings();class YearWorker extends Employee 【代码1】 /重写earnings()方法class MonthWorker extends Employee 【代码2】 /重写earnings()方法。class WeekWorker extends Employee 【代码3】 /重写earnings()方法。class Company Employee employee; double salaries=0; Company(Employee employee) this.employee=employee; public double salariesPay() salaries=0; 【代码4】 /计算salaries。 return salaries; public class HardWork public static void main(String args) Employee employee=new Employee20; for(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(); package chengxu;abstract class Employee public abstract double earnings();class YearWorker extends Employee public double earnings() return 1.0;class MonthWorker extends Employee public double earnings() return 2.0;class WeekWorker extends Employee public double earnings() return 3.0;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 += employeei.earnings();return salaries;public class HardWork public static void main(String args) Employee employee = new Employee20;for (int i = 0; i employee.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();3、读懂下面模板代码,按要求补充程序并调试运行。掌握接口的定义及其实现方法,学习接口回调的运用方法。interface ComputerWeight public double computeWeight();class Television implements ComputerWeight 【代码1】 /实现computeWeight()方法。class Computer implements ComputerWeight 【代码2】 /实现computeWeight()方法。 class WashMachine implements ComputerWeight 【代码3】 /实现computeWeight()方法。class Car ComputerWeight goods; double totalWeights=0; Car(ComputerWeight goods) this.goods=goods; public double getTotalWeights() totalWeights=0; 【代码4】 /计算totalWeights return totalWeights; public class Road public static void main(String args) ComputerWeight goodsOne=new ComputerWeight50, goodsTwo=new ComputerWeight22 ; for(int i=0;igoodsOne.length;i+) if(i%3=0) goodsOnei=new Television(); else if(i%3=1) goodsOnei=new Computer(); else if(i%3=2) goodsOnei=new WashMachine(); for(int i=0;igoodsTwo.length;i+) if(i%3=0) goodsTwoi=new Television(); else if(i%3=1) goodsTwoi=new Computer(); else if(i%3=2) goodsTwoi=new WashMachine(); Car 大货车=new Car(goodsOne); System.out.println(大货车装载的货物重量:+大货车.getTotalWeights(); Car 小货车=new Car(goodsTwo); System.out.println(小货车装载的货物重量:+小货车.getTotalWeights(); interface ComputerWeight public double computeWeight();class Television implements ComputerWeight public double computeWeight() return 1;class Computer implements ComputerWeight public double computeWeight() return 2;class WashMachine implements ComputerWeight public double computeWeight() return 3;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 += puteWeight();return totalWeights;public Test public static void main(String args) ComputerWeight goodsOne = new ComputerWeight50, goodsTwo = new ComputerWeight22;for (int i = 0; i goodsOne.length; i+) if (i % 3 = 0)goodsOnei = new Television();else if (i % 3 = 1)goodsOnei = new Computer();else if (i % 3 = 2)goodsOnei = new WashMachine();for (int i = 0; i goodsTwo.length; i+) if (i % 3 = 0)goodsTwoi = new Television();else if (i % 3 = 1)goodsTwoi = new Computer();else if (i % 3 = 2)goodsTwoi = new WashMachine();Car 大货车 = new Car(goodsOne);System.out.println(大货车装载的货物重量: + 大货车.getTotalWeights();Car 小货车 = new Car(goodsTwo);System.out.println(小货车装载的货物重量: + 小货车.getTotalWeights();大货车装载的货物重量:99.0小货车装载的货物重量:43.0 (六) 接口的实现与运用实验任务 :本实验的任务是设计和实现一个 Soundable 接口 , 该接口具有发声功能 , 同时还能够调节声音大小。 Soundable 接口的这些功能将会由 3 种声音设备来具体实现 , 它们分别是收音机 Radio 、随身昕 Walkman 和手机 Mobilephone 。最后还要设计一个应用程序类来使用这些实现了 Soundable 接口的声音设备类。程序运行时 , 先询问用户想听哪种设备 , 然后程序就会按照该设备的工作方式来发出声音。实验步骤 :(1) 仔细阅读程序清单1-9, 并完成其中的代码1代码3。程序清单1-9 InterfaceTest.javaimport java.util.Scanner;interface Soundable public void increaseVolume( ); public void decreaseVolume( ); public void stopSound( ); public void playSound( );class Radio implements Soundable public void increaseVolume( ) System.out.println(增大收音机音量); public void decreaseVolume( ) System.out.println(减小收音机音量); public void stopSound( ) System.out.println(关闭收音机); public void playSound( ) System.out.println(收音机播放广播); class Walkman implements Soundable public void increaseVolume( ) System.out.println(增大随声听音量); public void decreaseVolume( ) 代码1 / 输出减小随声听音量 public void stopSound( ) System.out.println(关闭随声听); public void playSound( ) System.out.println(随声听发出音乐); class Mobilephone implements Soundable public void increaseVolume( ) System.out.println(增大手机音量); public void decreaseVolume( ) System.out.println(减小手机音量); public void stopSound( ) System.out.println(关闭手机); public void playSound( ) System.out.println(手机发出来电铃声); class People private String name; private int age; public void listen(Soundable s) s.playSound( ); pu

温馨提示

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

评论

0/150

提交评论