java第五次实验.doc_第1页
java第五次实验.doc_第2页
java第五次实验.doc_第3页
java第五次实验.doc_第4页
java第五次实验.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

南昌航空大学实验报告2016 年 4 月 12 日课程名称:Java语言程序设计 实验名称: 继承与接口(一) 学号: 14205204 姓名: 王颖 同组人: 指导教师评定: 签名: 一、实验目的1、 掌握Java语言中继承的基本概念及使用方法;2、 掌握Java语言中成员变量隐藏的基本概念及使用方法;3、 掌握Java语言中super关键字的使用方法;4、 理解继承与组合的区别;5、 理解程序设计中代码复用的重要性。二、实验要求1、 根据实验步骤中提出的要求,使用记事本编写相应的Java程序;2、 使用JDK中提供的javac以及java命令编译、运行自己所编写的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,并在该类中实现主方法,其代码如下:class 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.print(); (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;=name;this.sex=sex;this.age=age; public void display()System.out.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 display()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方法改为: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)=name; class Son extends FatherString name;public Son(String name,String fatherName)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.username=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, 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(+老师登录+(flag?成功!:失败!); (4) 如果每位教师都有两个账号,请采用组合的方式编写一个程序实现教师的登录。以下是主方法的代码: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.Permission;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); = name;和System.out.printf(name+”的父亲是”+F);4、(1)继承:class Circledouble r;public Circle(double r) this.r=r; public double Area() return Math.PI*r*r; class Cylinder extends Circle double high; public Cylinder(double r,double high) super(r); this.high=high; public double Area() return (2*Math.PI*r*high+ super.Area()*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 Circle().r;return (Area() * 2 + 2 * Math.PI * r * high);public double Volume() return Area() * high;组合:优点:不破坏封装,整体类与局部类之间松耦合,彼此相对独立。具有较好的可扩展性。支持组合。在运行时,整体对象可以选择不同类型的局部对象。整体类可以对局部类进行包装,封装局部类的接口,提供新的接口。缺点:整体类不能自动获得和局部类同样的接口。创建整体类的对象时,需要创建所有局部类的对象。继承: 优点:子类能自动继承父类的接口。创建子类的对象时,无须创建父类的对象。缺点:破坏封装,子类与父类之间紧密耦合,子类依赖于父类的实现,子类缺乏独立性。支持扩展,但是往往以增加系统结构的复杂度为代价。不支持动态继承。在运行时,子类无法选择不同的父类。 (2)(3)在主方法之前加上:import java.util.Scanner;class Teacher extends Account String name;public Teacher (String name, String username, String password) super(username, password); = name;(4) import java.util.Scanner;public class Teacher String name;Account account1;Account account2;private Account account;public Teacher(String name, Account account1,Account account2) this.account1 = account1;this.account2 = account2; = name;private boolean login(String username, String password) return (username.equals(this.account1.username)&(password.equals(this.account1.password) |(username.equals(this.account2.username)&(password.equals(this.account2.password);public static void main(String args) Account accounts = new Account(1, 1), new Account(2, 2) ;Teacher teacher = new Teacher(李明, accounts0,accounts1);Scanner reader = new Scanner(System.in);System.out.println(请输入用户名:);Str

温馨提示

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

评论

0/150

提交评论