类的继承关系.doc_第1页
类的继承关系.doc_第2页
类的继承关系.doc_第3页
类的继承关系.doc_第4页
类的继承关系.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

第 八 周实验学号 20082433 姓名 张立健 专业 网络工程 一、已完成实验(仿照下面给出本次实验的所有内容)实验一1、实验内容 阅读如下所示的3个Java类的定义,分析它们之间的关系,写出运行结果。 2、程序运行结果(给出编译、运行的截图) 3、程序清单 class SuperClass int x; SuperClass() x=3;System.out.println(in SuperClass : x= +x); void doSomething() System.out.println(in SuperClass.doSomething(); class SubClass extends SuperClass int x; SubClass() super(); / 调用父类的构造方法 x=5; / super( ) 要放在方法中的第一句 System.out.println(in SubClass :x=+x); void doSomething( ) super.doSomething( ); / 调用父类的方法 System.out.println(in SubClass.doSomething(); System.out.println(super.x=+super.x+ sub.x=+x); public class TestDemo public static void main(String args) SuperClass sup=new SubClass(); sup. doSomething(); SubClass sub = new SubClass(); sub.doSomething(); 实验二.一 1、实验内容:编写程序并验证。 2、程序运行结果(给出编译、运行的截图) 3、程序清单/EmployeeTest.javaimport java.util.*;public class EmployeeTest public static void main(String args) / 将三个员工对象的数据赋值给职工数组 Employee staff = new Employee3; staff0 = new Employee(张三, 75000,1987, 12, 15); staff1 = new Employee(李四, 50000,1989, 10, 1); staff2 = new Employee(王五, 40000,1990, 3, 15); for (int i = 0; i staff.length; i+) staffi.raiseSalary(5);/每个员工的工资增长5% for (int i = 0; i staff.length; i+)/ 打印输出员工信息 Employee e = staffi; System.out.println(姓名= + e.getName() + ,工资= + e.getSalary() + ,工作日期= + e.getHireDay(); class Employee private String name; private double salary; private Date hireDay; public Employee(String n, double s, int year, int month, int day) name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); / GregorianCalendar 计算月份从0开始 hireDay = calendar.getTime(); public String getName() return name; public double getSalary() return salary; public Date getHireDay() return hireDay; public void raiseSalary(double byPercent) double raise = salary * byPercent / 100; salary += raise; 实验二.二 2.实验运行结果:3.实验源代码:import java.util.*;public class ManagerTest public static void main(String args) / 创建一个新的经理并设置他的奖金 Manager boss = new Manager(陈鹏, 80000, 1987, 12, 15); boss.setBonus(5000); Employee staff = new Employee3; / 建立员工数组和经理一起填充 staff0 = boss; staff1 = new Employee(何恒, 50000, 1989, 10, 1); staff2 = new Employee(童同, 40000, 1990, 3, 15); / print out information about all Employee objects for (int i = 0; i 0 ? s : 0 ); / 确定Boss的薪水 public double earnings() return weeklySalary; / 打印姓名 public String toString() return 经理: + super.toString(); public static void main(String args) Boss boss=new Boss(li,lei,2000); System.out.println(name= + boss.toString()+ ,salary= + boss.earnings(); 实验三 1、实验内容:假定根据学生的6门学位课程的分数决定其是否可以拿到学位,对于本科生,如果6门课程的平均分数超过60分即表示通过,而对于研究生,则需要平均超过80分才能够通过。根据上述要求,请完成以下Java类的设计:(1)设计一个基类Student描述学生的共同特征。(2)设计一个描述本科生的类Undergraduate,该类继承并扩展Student类。(3)设计一个描述研究生的类Graduate,该类继承并扩展Student类。(4)设计一个测试类StudentDemo,分别创建本科生和研究生这两个类的对象,并输出相关信息。2、程序运行结果(给出编译、运行的截图) 3、程序清单class Student public String studentNumber; public String classNumber;public String studentName;public String studentSex;public int studentAge;public Student(String a,String b,String c,String d,int e) studentNumber=a; classNumber=b; studentName=c; studentSex=d; studentAge=e;public String toString() String s=studentNumber:+studentNumber+nsudentName:+studentName+nclassNumber:+classNumber+nstudentSex:+studentSex+nstudentAge:+studentAge; return s;class Undergraduate extends Studentdouble score;Undergraduate(String a,String b,String c,String d,int e,double scores) /*studentNumber=a; classNumber=b; studentName=c;studentSex=d; studentAge=e;*/ super(a,b,c,d,e); this.score=new double6; for(int i=0;i6;i+) scorei=scoresi; public void pass() double average,scoresum=0; for(int i=0;i=60) System.out.println(该生通过!); else System.out.println(该生未通过!);class Graduate extends Studentdouble score; Graduate(String a,String b,String c,String d,int e,double scores) /*studentNumber=a; classNumber=b; studentName=c;studentSex=d; studentAge=e;*/ super(a,b,c,d,e); this.score=new double6; for(int i=0;i6;i+) scorei=scoresi; public void pass() double average,scoresum=0; for(int i=0;i=80) System.out.println(该生通过!); else System.out.println(该生未通过!);public class StudentDemopublic static void main(String args) double b=new double6;double y=new double6;b0=58.0;b1=78.0;b2=94.0;b3=65.0;b4=49.0;b5=60.0;y0=90.0;y1=85.0;y2=96.0;y3=87.0;y4=91.0;y5=90.0;Undergraduate u=new Undergraduate(20082433,class2,zhanglijian,nv,22,b);Graduate g=new Graduate(20081019,class1,caizhanwang,nan,24,y);System.out.println(the undergraduate basic information :+u.toString();u.pass();System.out.println(the graduate basic information:+g.toString();g.pass();实验四1. 实验内容:2. 实验运行结果: 3. 实验源代码: abstract class Shape double length1;abstract double getArea();abstract void displayArea();class Trangle extends Shapedouble length2;double s;Trangle(double a,double b) this.length1=a; this.length2=b;double getArea()s=length1*length2; return s;public void displayArea() System.out.println(the area of the trangle is:+getArea();class Triangle extends Shapedouble high;Triangle(double a,double b)this.length1=a;this.high=b;double getAr

温馨提示

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

评论

0/150

提交评论