《Java实验报告样本.doc》由会员分享,可在线阅读,更多相关《Java实验报告样本.doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、信息科学与工程学院 Java程序设计 上机实验报告专业班级 电技电信0901 姓 名 刘雄楚 学 号 0 实验时间 2011 年 9 月 30 日 指导教师 程骅 成 绩 实验名称面向对象程序设计的继承、多态等特性的练习实验目的1) 掌握类的继承机制。2) 熟悉类中成员变量和方法的访问控制。3) 熟悉方法或构造方法多态性。4) 熟悉接口的定义方法。1class Personpublic String name;public char sex;public int age; Person(String name,char sex,int age)this.name=name;this.sex=s
2、ex;this.age=age;class Student extends Personpublic String classno;public String sno;public void updateAge(int age) this.age=age;public String toString()System.out.println(this.name+ +this.sex+ +this.age+ +this.classno+ +this.sno);return this.name+ +this.sex+ +this.age+ +this.classno+ +this.sno;Stude
3、nt(String name,char sex,int age,String classno,String sno)super(name,sex,age);this.classno=classno;this.sno=sno;public class TestStudentPersonpublic static void main(String args)Student s1=new Student(jeck,n,20,2);s1.updateAge(21);s1.toString();2public class TestVirtualInvoke static void doStuff(Sha
4、pe s) s.draw(); public static void main(String args) Circle c=new Circle(); Triangle t=new Triangle(); Line l=new Line(); doStuff(c); doStuff(t); doStuff(l); class Shapevoid draw()System.out.println(Shape Drawing);class Circle extends Shapevoid draw()System.out.println(Shape Circle);class Triangle e
5、xtends Shapevoid draw()System.out.println(Shape Triangle);class Line extends Shapevoid draw()System.out.println(Shape Line); 使用抽象类abstractabstract class Shapeabstract void draw(); class Circle extends Shapevoid draw()System.out.println(Shape Circle); class Triangle extends Shapevoid draw()System.out
6、.println(Shape Triangle); class Line extends Shapevoid draw()System.out.println(Shape Line);使用interface接口方法interface Shapepublic void draw(); class Circle implements Shape publicvoid draw()System.out.println(Shape Circle); class Triangle implements Shape public void draw()System.out.println(Shape Tr
7、iangle); class Line implements Shape publicvoid draw()System.out.println(Shape Line); 思考题:import java.util.*;abstract class Employee String name; long no; static long id=1000; abstract double getPay(); Employee(String name) this.name=name;id=id+1; ;class Manager extends Employee double pay=8000;doub
8、le getPay()return 8000; Manager(String name)super(name); no=id; public String toString()return name:+name+ no:+no+ pay:8000;class Sale extends Employee long saleNum; double getPay()return saleNum*0.04;Sale(String name,long saleNum)super(name); no=id; this.saleNum=saleNum; public String toString()ret
9、urn name:+name+ no:+no+ pay:+getPay();class Tech extends Employee long salaryHours; double getPay()return salaryHours*100; Tech(String name,long salaryHours)super(name); no=id; this.salaryHours=salaryHours; public String toString()return name:+name+ no:+no+ pay:+getPay();class EmployApp public stati
10、c void main(String args)Vector s=new Vector();Employee em;double numPay=0;s.addElement(new Sale(aa,);s.addElement(new Sale(bb,);s.addElement(new Tech(cc,35);s.addElement(new Tech(dd,30);s.addElement(new Manager(ee);Enumeration e=s.elements();while(e.hasMoreElements() em=(Employee)e.nextElement();numPay=numPay+em.getPay(); System.out.println(em);System.out.println(总工资:+numPay);心得:此次试验更多更广的运用了类的继承这一特性,使得程序更简单易读,同时运用到了抽象类与接口的使用方法,总体而言是上一次实验的引申,进一步加深了知识的运用,学习了更多的方法以及构造方法的运用方式。