Java程序设计:实验五.docx
实验五面向对象程序设计二【实验目的】.掌握Java一维数组的定义、使用方法。1 .理解继承的概念并学会使用。2 .掌握this, super, static, final等关键字的使用方法。3 .掌握方法的重载与方法的重写的区别。【实验准备】一、复习配套教材相关章节的内容;二、预习本次实验;【实验内容】1、编写一个程序用于将10同学的成绩保存在数组中,并计算这些同 学的平均分和总成绩。要求分别建立求平均值的方法和求总成绩的 方法。在主程序中创立10同学的成绩,并调用上述两方法,输出平 均成绩和总成绩。public static void main(String args) doublet scores = 55, 62, 98, 44, 89, 96, 77, 88, 100, 80 ;double sum = caLcSum(scores);double avg = caLc4Vg(scores);Sy stem. out. print In ("学生成绩的总和为:"+sum);System.out. print In ("学生成绩的平均分为:"+avg);private static double calcAvg(double scores) return caLcSum(scores') / scores.length;private static double calcSum(double scores) double sum = 0;for (int i = 0; i < scores.length; i+) sum += scoresi; return sum;2.编译下面程序,看报什么错误,为什么?如何改正?class StaticErrorString mystring= "hello” ;public static void main(String args) System, out. printin(mystring);错误:Exception in thread "main" java.lang.Error: Unresolved compilation problem:Cannot make a static reference to the non-static field mystringat test.StaticError.main(StaticError.java:6)原因:类是一般特性的描述,是一个抽象的概念。只有通过new关键字才会 产生出对象,这样对象才可以供外部调用。所以在对类中的成员变量和成员 方法进行调用之前,一定不要忘了创立该类的对象。修改如下:public class StaticError String mystring="hello"public static void main(String args) StaticError s = new StaticError();System.out.printIn(s.mystring);3、编写一个程序,模拟驾校,父类Employee,两个子类Manager 和Directoro父类Employee类包含3个属性和一个方法,属性为 name (姓名),basic (岗位)和address (地址),方法名为show,用 于显示这些属性值。Manager类有一个名为department (部门)的 属性,Directoro类有一个名为transport (车型)的附加属性。1)编写测试类,创立Manager和Director类对象,调用show () 方法,显示其详细信息。2)假设需要显示子类的全部信息,该如何修改程序。package test;public class test public static void main (String args)Employee e=new Employee ("小丽","员工","淮安”); 创立一个 Employee 对象System. out. printin ("Employee 的信息:"); e.show();Manager m=new Manager (“小明“J经理“,“淮安”开发部“);/ 创立个Ma n a ge对象Sy stem. out. print In ("nManager 的信息:");m.show(); 输出信息Director d =new Director (“小刚“J主管“,“南京“,“宝马”);/ 创立一个di rector对象System .out . printin ("nDirector 的信息:");调用函数输出信 息d.show();/输出信息 class Employee /创立Employee类 String name ;String basic; String address;Employee (String nam,String bas,String add) 初始化 this.name = nam;this.basic = bas; this.address = add; public void show ()System.out.printin("名字:"+this.name);System.out.printIn("岗位:"+this.basic);System. out . print In (" 地址:"+this. address); class Manager extends Employee"Manager类继承了父类的属性和方法String department;Manager (String nam. String bas. String add,String dep) super(nam, bas, add); 使用super关键字 department=dep; public void show() super.show();System.out.printIn("®fJ: "+department); class Director extends Employee/Director类继承了父类的属性和方法String transport;Director(String nam. String bas. String add,String tra) super(nam, bas, add); 使用super关键字 transport=tra; public void show() super.show();System.out.printin("车型:"+transport); )输出结果为:Employee的信息:名字:小丽岗位:员工地址:淮安Manager的信息:名字:小明岗位:经理地址:淮安部门:开发部Director的信息:名字:小刚岗位:主管地址:南京 车型:宝马4、编写一个程序,用于重写父类Addition中名为add()的方法,父 类中该方法没有实质性的操作,仅输出一条信息。add()方法在 NumberAddition类中将两个整数相加,而在TextConcatenation类 那么连接两个String字符串。创立主类测试两个子类中的add ()方法。 package labl;public class test public static void main(String args) Addition a=new Addition。;a.add();NumberAddition al=new NumberAddition ();al.add(7,8);Textconcatenation a2=new TextConcatenation(); a2.add("7"/'8");)class Addition public void add()System.out.printin(“该方法没有实质性的操作,仅输出一条信息“); class NumberAddition extends Addition double result;public void add(double numberl,double number2) result=numberl+number2;System. out. print In ("两个数相加为 “+result); )class Textconcatenation extends Addition String result2;public void add(String numbers,String number4) result2=number3+number4;System, out. printin("连接字符串为"+result2);)运行结果为:该方法没有实质性的操作,仅输出一条信息两个数相加为15.0连接字符中为78【总结与体会】通过本次试验得出继承中最常使用的两个关键字是extends和 implementso通过使用这两个关键字,我们能实现一个对象获取另 一个对象的属性。通过方法的重载与方法的重写,知道了重载的方 法的参数列表是必须修改的,而重写方法是一定不能修改的。