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

下载本文档

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

文档简介

1、山西大学计算机与信息技术学院实 验 报 告姓 名学 号专业班级课程名称Java实验实验日期成 绩指导教师批改日期实验名称实验6继承与接口实验目的1、掌握java继承中父类及其子类的定义方法。2、掌握子类重写父类同名方法的方法。3、掌握接口的用法。学习如何定义接口 ;掌握接口的实现方式;使用实现了接口的类;理解接口与抽象类的区别。实验要求1、复习理论教学中所学的内容。2、认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。3、认真总结实验并书写实验报告。实验内容1、类的继承性练习(1)程序源代码如下。public class Student(protected String xm; /姓

2、名,具有保护修饰符的成员变量protected int xh;/学号void setdata(String xm,int xh)设置数据的方法(this.xm=xm;this.xh=xh;public void print() /输出数据的方法(System.out.println(xm+,+xh);package T6;import java.util.*;public class LX6_1 extends Studentpublic LX6_1();public static void main(String args) Scanner input=new Scanner(System.

3、in); LX6_1 text=new LX6_1();System. out .println(请输入姓名和学号:);text.setdata(input.next(), input.nextInt();System.out.println(显示如下:); text.print();(2)编译源并运行程序。贴图如下F闻 C:Windowssystem32cmd.exeE: KaitoliXworkspaceSnew pro jectXsrc XT6jauac LX6_1. jauaE: Ka.itoliwovkspace Snew pro ject spc T6 java LX6_1 商输入

4、姓名和学号:Sminth100显示如下:Sminth, 100E: XKa.itoliXwovkspace Snew pro ject spc T6.(二)创建将被继承的类(1)程序功能:通过Student类产生子类CollegeStudent,其不仅具有父类的成员变量xm(姓名)、 xh (学号),还定义了新成员变量xy (学院)、bj (班级)。在程序中调用了父类的print方法, 同时可以看出子类也具有该方法。程序代码package T6;public class CollegeStudent extends Student private String xy;private Strin

5、g bj;public CollegeStudent() ;public void setdata(String xm, int xh, String xy, String bj) super.setdata(xm, xh);this.xy = xy;this.bj = bj;public void print() super.print();System.out.println(xy + , + bj);package T6;import java.util.*;public class LX6_2 public static void main(String args) Scanner i

6、nput = new Scanner(System.in);CollegeStudent new1 = new CollegeStudent();System.out.println(请输入姓名、学号、学院、班级:);new1.setdata(input.next(), input.nextInt(), input.next(), input.next();System.out.println(显示如下:);new1.print();运行结果贴图:r囱j 莒理员:C:Win dowsXsystem 3 2cmd. exeE: Ka.itoliworkspace ew pro ject spc

7、T6 java LX6_2 请输入姓名、学号、学院、班级; Sinth 100231Computer 1 辰示如下: Sinth, 100231 CompLLter, 1EjsKaJ_t oLiSoHspaejsnewpij.(三)了解成员方法的覆盖方式(1)编写覆盖了Object类toString方法的一个类,并用System.out.println()输出该类的一个 对象。程序代码package T6;import java.util.*;public class LX6_3 private String name;private int age;private int sex;priva

8、te String work;public LX6_3(String name, int age, int sex, String work) = name; this.age = age; this.sex = sex; this.work = work;public String toString() if (sex = 0) return ”姓名:+ name + 年龄:+ 性别:男”+ 职业:+ work; elsereturn ”姓名:+ name + 年龄:+ 性别:女”+ 职业:+ work; public static void main(String ar

9、gs) Scanner input = new Scanner(System.in); System.out.println(请输入姓名,年龄,性别(0=男,其他=女),工作:”); LX6_3 person = new LX6_3(input.next(), input.nextInt(), input.nextInt(), input.next();System.out.println(person);运行结果贴图: else return false;public String toString() System.out.println(super.toString();return (

10、x= + x + y= + y);package T6;public class TestPoint public static void main(String args) Point a = new Point(3, 2);Point d = new Point(3, 21);Point b = new Point(a);Object c = a.clone();System.out.println(a.equals(d);System.out.println(a.equals(c);System.out.println(a + a);System.out.println(b + b);S

11、ystem.out.println(c + c);/ 因为clone定义是new的为Point,所以为Point 类运行结果贴图:this、super和super()的使用程序功能:程序功能:说明this、super和super()的用法。程序首先定义Point2 (点)类, 然后创建点的子类Line (线)。最后通过TestLine类输出线段的长度。程序中通过super(a,b) 调用父类Point的构造方法为父类的x和y赋值。在子类Line的setLine方法中,因为参数 名和成员变量名相同,为给成员变量赋值,使用this引用,告诉编译器是为当前类的成员变 量赋值。在length和toSt

12、ring方法中使用父类成员变量时,使用super引用,告诉编译器 使用的是父类的成员变量。程序代码:package T6;public class Point2 protected double x;protected double y;public Point2(double x, double y) this.x = x;this.y = y;public String toString() return ;package T6;import java.util.*;public class Line extends Point2 private double x2, y2;public

13、Line(double x, double y) super(x, y);/这个值仍然付给line的x,y,所以line1.x会改此值public void setLine(double x2, double y2) this.x2 = x2;this.y2 = y2;public double length() double length;length = Math.sqrt(x2 - super.x) * (x2 - super.x) + (y2 - super.y)* (y2 - super.y);return length;public String toString() return

14、 ”起点坐标:(+ super.x + , + super.y + ) 终点坐标为:(”+ x2 + ”,”+ y2 + ) 长度为:+ length();public static void main(String args) Scanner input = new Scanner(System.in);System. out .println(请输入起点坐标”);Line line1 = new Line(input.nextDouble(), input.nextDouble();System. out .println(请输入终点坐标”);line1.setLine(input.nex

15、tDouble(), input.nextDouble();System.out.println(line1);运行结果贴图:(五)接口的实现与运用实验任务:本实验的任务是设计和实现一个Soundable接口,该接口具有发声功能,同时还能够调节声 音大小。Soundable接口的这些功能将会由3种声音设备来具体实现,它们分别是收音机 Radio、随身昕Walkman和手机Mobilephone。最后还要设计一个应用程序类来使用这些实现了Soundable接口的声音设备类。程序运行时,先询问用户想听哪种设备,然后程序就会按照该设备 的工作方式来发出声音。实验步骤:(1)仔细阅读程序,并完成其中的

16、代码广代码3。import java.util.Scanner;import java.util.concurrent.CancellationException; interface Soundable public void increaseVolume();public void decreaseVolume();public void stopSound();public void playSound();class Radio implements Soundable public void increaseVolume( ) System.out.println(增大收音机音量);

17、 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( ) Syste

18、m.out.println(减小收音机音量”); 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 s

19、topSound( ) System.out.println(关闭手机”);public void playSound( ) System.out.println(手机发出来电铃声”);class People private String name;private int age;public void listen(Soundable s) s.playSound();public class InterfaceTest public static void main(String args) int i;People sportsman = new People();Scanner sc

20、anner = new Scanner(System.in);Soundable soundDevice = new Soundable3;往声音设备数组中放入能发声的设备soundDevice0 = new Radio();soundDevice1 = new Walkman();soundDevice2=new Mobilephone();System.out.println(你想听什么?请输入选择:0-收音机1-随声听2-手机”);i = scanner.nextInt();开始听声音sportsman.listen(soundDevicei);soundDevicei.increase

21、Volume();soundDevicei.stopSound();打开文本编辑器编辑InterfaceTest.java并保存,然后在Eclipse环境中进行编译, 编译的结果将会产生6个class文件,其中包括Soundable.class,虽然Soundable本身是一 个接口,但编译之后也会产生class文件。编译之后运行这个程序,观察所得结果。思考请问在InterfaceTest类中,SoundDevice数组是什么类型的,该数组为什么能存放 3 种不同的对象 Radio、Walkman 和 Mobilephone 呢?答Soundrable接口类型,在内存中开辟了三个空间用来存放s

22、oundable类型的对象引用,因为Radio, Walkman 和 Mobilephone都实现了Soundrable接口在程序中,Soundable是一个接口 ,那么该接口是否可以被实例化呢?请在 InterfaceTest类的main()方法中加入以下语句试验一下,并分析结果。Soundable Sound=new Soundable(),答:接口和抽象类不能被实力化。现在假定要为程序增加一个闹钟类Clock,该类也实现Soundable接口,能够发出滴答 声,请将以下的 Clock类加入到InterfaceTest.java程序中,并在InterfaceTest类的 main()方法中

23、加入 SoundDevice3 new Clock();语句。class Clock implements Soundablepublic void Stopsound()System.out.println(关闭闹钟);public void Playsound()system.out.println(闹钟发出滴答声);修改之后,重新编译InterfaceTest.java并运行它,观察结果。在第(3)小题中由于新加入的Clock类仅仅实现了 Soundable接口的stopsound()和 playsound()方法,而increaseVolume()和decreaseVolume()方法

24、没有实现,因此它实质上 是一个抽象类,而抽象类是不能实例化的,所以导致编译错误。但是按照常理,闹钟的滴答声确 实是不可以增大或减小的,那么如何解决这个问题呢?现在请在Clock类中加入下面两个含 空方法体的方法实现,再编译运行程序,看看会有什么变化。public void increaseVolume()public void decreaseVolume()答:可以运行了。现在请模仿本实验的程序设计出一个自己的接口程序,要求先设计一个moveable可移动 接口,然后分别设计3个类,即汽车Car、轮船Ship、飞机Aircraft来实现该接口,最后 设计一个应用程序来使用它们。程序代码:pa

25、ckage T6;public interface Moveable public void increasespeed();public void decreasespeed();public void start();public void stop();package T6;public class Car implements Moveablepublic void increasespeed()(System.out.println(汽车加速!”);public void decreasespeed()(System.out.println(汽车减速!”);public void s

26、top()(System.out.println(汽车停止!”);public void start()System. out.println(汽车启动!”);package T6;public class Ship implements Moveablepublic void increasespeed()(System.out.println(速度增加! ”);public void decreasespeed()(System.out.println(速度减少!”);public void start()System.out.println(轮船启动!);public void stop

27、()(System.out.println(轮船停止!”);package T6;public class Aircraf implements Moveable public void increasespeed()(System.out.println(速度增加!”);public void decreasespeed()(System.out.println(速度减小!”);public void start()System.out.println(飞机启动!”);public void stop()(System.out.println(飞机停止!”);package T6;publi

28、c class People private String name;private int age;public void take(Moveable s) s.start();package T6;import java.util.*;public class MoveTest public static void main(String args) int i;People businessMan = new People();Scanner input = new Scanner(System.in);Moveable newmove = new Moveable3;newmove0

29、= new Car();newmove1 = new Ship();newmove2 = new Aircraf();System.out.println(你想乘坐什么?请输入选择:0-汽车1-船2-飞机”);i = input.nextInt();businessMan.take(newmovei);newmovei.increasespeed();newmovei.decreasespeed();newmovei.stop();运行结果贴图:莒理贝;C:Wtn dowsXsystem 3 2cmd. exe1=1回空:XKa.itoliworkspace ew pro ject si*c Tb jauac MoueTest - jaua:KaitoliXwopkspace Snew pro ject src T6 jaua MoueTestX想乘坐什么?请输入选择:昨汽车船2-飞机动速速止IsKajtoLiwor4space Sn e wprb 实验分析:接口的特点在于只定义

温馨提示

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

评论

0/150

提交评论