JAVA第三次上机实验报告
信息科学与工程学院 《Java程序设计》 上机实验报告
专业班级 电技电信0902 姓 名 曾雪婷 学 号 200912135049
实验时间 2010 年 9 月 30 日 指导教师 程骅 成 绩
实验名面向对象程序设计的继承、多态等特性的练习 称
掌握类的继承机制。
实验目熟悉类中成员变量和方法的访问控制。 的 熟悉方法或构造方法多态性。
熟悉接口的定义方法。
1)编写程序实现如下功能:已知Person类包含三个公共域变量(姓名、性别、年龄)和一个
构造方法,Student类是Person类的派生类,包含两个新的公共域变量(学号、班号)、两个公共主
方法(构造方法、修改年龄、显示基本信息)和一个构造方法。定义一组学生对象,并初始化他要
们的基本信息,然后依次输出。 实 源程序: 验 class Person 记 {
录 public String name;
及 public String sex;
public String age; 个
Person(String name,String sex,String age) 人
{ 小 this.name=name; 结 this.sex=sex;
) this.age=age;
包 } 括} 部
class Student extends Person 分
实 { 验 public String sno; 源
public String classno; 程
序 public void updateAge(String age) 、 { 调 this.age=age; 试
结 }
果 及 public void printString() 实
{ 验
结 System.out.println(" 学号:" +sno+ " 班号:" +classno+ " 姓名:" +name+ " 性果别:" +sex+ " 年龄:" +age); 分
} 析
等 Student(String sno,String classno,String name,String sex,String age) ) {
super(name,sex,age);
this.sno=sno;
this.classno=classno;
1 注:“实验记录及个人小结”部分不够可另附页或在背面续写 第 页
}
}
public class TestStudentPerson
{
public static void main(String[] args)
{
int i;
Student a[]=new Student[5];
a[0]=new Student("101","01","李明","男","18");
主 a[1]=new Student("102","06","王芳","女","19");
要 a[2]=new Student("103","03","刘希","女","20");
a[3]=new Student("104","02","廖刚","男","18"); 实
a[4]=new Student("105","01","马峰","男","21"); 验
for(i=0;i<=5;i++) 记 a[i].printString(); 录 } 及 }
个 运行结果:
人
小
结
)
包
括
部
分
实
验
源
程 序2) 输入课本例5-4的源程序,观察其运行结果,然后将Shape类分别定义为一个抽象类和接口,、然后将源程序进行修改,让其实现相同功能。 调
试 ? 源程序:
结 class TestVirtualInvoke 果{ 及
实 static void doStuff(Shape 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 Shape
{ void draw()
2 注:“实验记录及个人小结”部分不够可另附页或在背面续写 第 页
{System.out.println("Shape Drawing");}
}
class Circle extends Shape
{ void draw()
{System.out.println("Draw Circle");}
}
class Triangle extends Shape
{ void draw()
{System.out.print ln("Draw Three Lines");} 主 } 要 class Line extends Shape 实 { void draw()
验 {System.out.println("Draw Line");}
} 记
运行结果: 录
及
个
人
小
结
)
包
括
部 分?将Shape类定义为abstract class Shape{……},然后重新定义Shape抽象类的派生类Circle、实
验Triangle、Line:
源源程序: 程class TestVirtualInvoke 序
{ 、
调 static void doStuff(Shape s){ 试 s.draw(); 结
} 果
及 public static void main(String[] args){ 实 Shape c=new Circle(); 验 Shape t=new Triangle(); 结
果 Shape l=new Line();
分 doStuff(c); 析 doStuff(t); 等
) doStuff(l);
}
}
abstract class Shape
{ void draw()
{System.out.println("Shape Drawing");}
}
class Circle extends Shape
{ void draw()
3 注:“实验记录及个人小结”部分不够可另附页或在背面续写 第 页
{System.out.println("Draw Circle");} }
class Triangle extends Shape
{ void draw()
{System.out.println("Draw Three Lines");} }
class Line extends Shape
{ void draw()
{System.out.println("Draw Line");} }
运行结果:
?将Shape定义为interface Shape{„„},然后基于Shape接口实现类Circle、Triangle、Line:
class TestVirtualInvoke
{
static void doStuff(Shape s){
s.draw();
}
public static void main(String[] args){
Shape c=new Circle();
Shape t=new Triangle();
Shape l=new Line();
doStuff(c);
doStuff(t);
doStuff(l);
}
}
interface class Shape
{ void draw();
}
class Circle implements Shape
{ void draw()
{System.out.println("Draw Circle");} }
class Triangle implements Shape { void draw()
{System.out.println("Draw Three Lines");}
4 注:“实验记录及个人小结”部分不够可另附页或在背面续写 第 页
}
class Line implements Shape
{ void draw()
{System.out.println("Draw Line");} }
思考
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
:
某小型公司,主要有三类人员:经理、兼职技术人员和兼职推销员。
这三类人员共同拥有的相关属性:姓名、编号; 相关方法:获取当月薪水的getPay()方法、显示个人信息的toString()方法。
人员编号基数为1000,每创建一个人员实例对象,其编号属性的值顺序加1。月薪计算方法:经理拿固定月薪8000元;兼职技术人员按每小时100元领取月薪;兼职推销人员的月薪按当月的销售额的4%提成。 要求:根据题意,自己确定需要定义的类及各类的属性、构造方法和其他方法,并编写Java Applcation程序实现功能:存储若干个职工信息,然后分行显示所有职工的姓名、编号、月工资等相关信息,另外计算所有职工的月薪总和并显示出来。
源程序:
class Person
{
String name;
static int num;
Person(String name,int num)
{
this.name=name;
this.num=num;
num++;
}
}
class Manager extends Person
{
Manager(String name,int num)
{
super(name,num);
}
public float getPay()
{
float salary;
salary=8000;
return salary;
}
public String toString()
{
return name+","+num+","+"getPay()";
}
5 注:“实验记录及个人小结”部分不够可另附页或在背面续写 第 页
}
class Technician extends Person {
float hour;
Technician(String name,int num,float hour)
{ super(name,num);
this.hour=hour;
}
public float getPay(float hour)
{
float salary;
salary=100*hour;
return salary;
}
public String toString()
{
return name+","+num+","+hour+","+"Technician.getPay()";
}
}
class Salesman extends Person
{
float money;
Salesman(String name,int num,float money)
{
super(name,num);
this.money=money;
}
public float getPay(float money)
{
float salary;
salary=(float)(money*0.04);
return salary;
}
public String toString()
{
return name+","+num+","+money+","+"getPay()";
}
}
public class PersonSalary
{
public static void main(String[] args)
{
Manager a=new Manager("李然",1000);
6 注:“实验记录及个人小结”部分不够可另附页或在背面续写 第 页
Technician b=new Technician("肖云",1000,90);
Salesman c=new Salesman("林超",1000,3000);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
程序未调试成功,目前编译通过,但是运行结果不对:
个人小结:
1( this()必须放在方法的第一句
2( 在接口中,所有的方法为公开、抽象的方法:public abstract 3( abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声
明的类型,也就是编译时类型
4( 接口中没有构造方法,也没有main方法
5( 任何一个类的对象访问到static修饰的域,去到的都是相同的值
7 注:“实验记录及个人小结”部分不够可另附页或在背面续写 第 页