java第五次实验

上传人:第*** 文档编号:34059364 上传时间:2018-02-20 格式:DOC 页数:8 大小:73.50KB
返回 下载 相关 举报
java第五次实验_第1页
第1页 / 共8页
java第五次实验_第2页
第2页 / 共8页
java第五次实验_第3页
第3页 / 共8页
java第五次实验_第4页
第4页 / 共8页
java第五次实验_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《java第五次实验》由会员分享,可在线阅读,更多相关《java第五次实验(8页珍藏版)》请在金锄头文库上搜索。

1、南昌航空大学实验报告2016 年 4 月 12 日课程名称:Java 语言程序设计 实验名称: 继承与接口(一) 学号: 14205204 姓名: 王颖 同组人: 指导教师评定: 签名: 一、实验目的1、 掌握 Java 语言中继承的基本概念及使用方法;2、 掌握 Java 语言中成员变量隐藏的基本概念及使用方法;3、 掌握 Java 语言中 super 关键字的使用方法;4、 理解继承与组合的区别;5、 理解程序设计中代码复用的重要性。二、实验要求1、 根据实验步骤中提出的要求,使用记事本编写相应的 Java 程序;2、 使用 JDK 中提供的 javac 以及 java 命令编译、运行自己

2、所编写的 Java 程序;3、 根据编译与运行过程中所获得的错误信息修改程序直至获得正确的结果;4、 记录实验中遇到的各类问题并以及解决办法。三、实验步骤1、 父类与子类(1) 编写一个基本的 Point 类,其代码如下:class Pointprivate double x;private double y;Point(double x, double y)this.x=x;this.y=y;void print()System.out.printf(x=%4.2f,y=%4.2fn,x,y);(2) 编写 Point 类的一个子类 ColorPoint,并在该类中实现主方法,其代码如下:c

3、lass ColorPoint extends Pointint colorType;final static char colors=R,G,B;ColorPoint(double x,double y, int colorType)super(x,y);this.colorType=colorType;void print()super.print();System.out.printf(color:%s,colorscolorType);public static void main(String args)ColorPoint cp=new ColorPoint(8,7,1);cp.p

4、rint();(3) 提问:ColorPoint 构造方法中的两条语句是否可以颠倒?为什么?2、 父类成员变量的访问(1) 已知有父类 Person 与子类 Student,其代码如下:class Personprivate String idNo;String name;boolean sex;int age;public Person(String idNo,String name,boolean sex,int age)this.idNo=idNo;this.name=name;this.sex=sex;this.age=age;public void display()System.o

5、ut.println(姓名: +name);System.out.println(性别: +(sex?男:女);System.out.println(年龄: +age);System.out.println(身份证: +idNo);class Student extends PersonString studentNo;public Student(String studentNo,String idNo,String name,boolean sex,int age)super(idNo,name,sex,age);this.studentNo=studentNo;public void d

6、isplay()System.out.printf(学号: %sn,studentNo);super.display(); class PersonTestpublic static void main(String args)Student st=new Student(10201198,36.,*,true,20);st.display();(2) 将代码编译并运行,考虑子类是如何访问父类的私有变量的?(3) 如果 Student 在 example.school 包中,而 Person 和 PersonTest 在 example 包中,而且将Person 的 display 方法改为:

7、public void display()System.out.printf(学号: %sn,studentNo);System.out.printf(姓名: %s*n,name.substring (0,1);(4) 那么其它的代码应该如何修改?3、 父类成员变量的访问(1) 已知有父类 Person 与子类 Student,其代码如下:class FatherString name;Father(String name)this.name=name;class Son extends FatherString name;public Son(String name,String fathe

8、rName)public void display()public static void main(String args)Son son=new Son(苏轼,苏洵);son.display();(2) 请将省略处的代码补充完整,使程序运行后显示结果“苏轼的父亲是苏洵”4、 继承与组合(1) 请分别采用继承的方式与组合的方式实现圆柱体,并比较两种方式的优劣。(2) 已知有一帐号类,其代码如下:class AccountString username;String password;public Account(String username,String password)this.use

9、rname=username;this.password=password;public boolean login(String username,String password)return username.equals(this.username) &password.equals(this.password);(3) 目前已知每位教师只有一个账号,请采用继承的方式编写一个程序实现教师的登录。以下是主方法的代码:public static void main(String args)Teacher teacher=new Teacher(李明,1,1);/(name, username

10、, password)Scanner reader=new Scanner(System.in);System.out.println(请输入用户名:);String username=reader.nextLine();System.out.println(请输入密码:);String password=reader.nextLine();boolean flag=teacher.login(username,password);System.out.println(teacher.name+老师登录+(flag?成功!:失败!);(4) 如果每位教师都有两个账号,请采用组合的方式编写一个程

11、序实现教师的登录。以下是主方法的代码:Account accounts = new Account(1,1),new Account(2,2);Teacher teacher=new Teacher(李明,accounts);(将上述代码替换第三步中的第一行)四、实验结果 1、 (1) (2)(3)不能,因为要先调用 x 和 y 的值。2、 (1)(2)用 super 调用了父类的构造方法,子类继承父类,所以能访问父类的私有变量。(3)(4)PersonTest 不变,将 Student 改为:package example.school;import java.security.Permis

12、sion;abstract class Student extends Permission String studentNo;public Student(String studentNo, String idNo, String name, boolean sex,int age) super(name);this.studentNo = studentNo;public void display() System.out.printf(学号: %sn, studentNo);3、(1)(2) 省略号应添加的分别是:super(fatherName);this.name = name;和

13、System.out.printf(name+”的父亲是”+Father.name);4、(1)继承:class Circledouble r;public Circle(double r)this.r=r;public double Area()return Math.PI*r*r;class Cylinder extends Circledouble high;public Cylinder(double r,double high) super(r);this.high=high;public double Area()return (2*Math.PI*r*high+ super.Ar

14、ea()*2);public double Volume()return super.Area()*high;组合:class Circle double r;public Circle() this.r = r;public double Area() return Math.PI * r * r;public class Cylinder protected Circle circle;protected double r;double high;public Cylinder() circle = new Circle();public double Area() r = new Cir

15、cle().r;return (Area() * 2 + 2 * Math.PI * r * high);public double Volume() return Area() * high;组合:优点:不破坏封装,整体类与局部类之间松耦合,彼此相对独立。具有较好的可扩展性。支持组合。在运行时,整体对象可以选择不同类型的局部对象。整体类可以对局部类进行包装,封装局部类的接口,提供新的接口。缺点:整体类不能自动获得和局部类同样的接口。创建整体类的对象时,需要创建所有局部类的对象。继承: 优点:子类能自动继承父类的接口。创建子类的对象时,无须创建父类的对象。缺点:破坏封装,子类与父类之间紧密耦合,子类依赖于父类的实现,子类缺乏独立性。支持扩展,但是往往以增加系统结构的复杂度为代价。不支持动态继承。在运行时,子类无法选择不同的父类。

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

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

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