实验三 继承和多态
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验三继承和多态
一、实验目的
1.熟练掌握封装性,多态性的概念。
2.熟练掌握抽象类abstract的概念。
3.熟练掌握接口interface的概念。
4.熟练包package的概念以及编译运行的方法。
二、实验内容
1.编写一个Java Application程序,该程序有个点Point类,它包含横坐标x和纵坐标y 两个属性,再给Point定义两个构造方法和一个打印点坐标的方法Show。
定义一个圆Circle 类,它继承Point类(它是一个点,圆心(Center)),除此之外,还有属性半径Radius,再给圆定义2个构造方法、一个打印圆的面积的方法PrintArea和一个打印圆中心、半径的方法Show(其中显示圆心可以用super.Show()的方式)。
package实验三;
public class Point {
double x,y;
public Point(){
x=0;
y=0;
}
public Point(double x,double y){
this.x=x;
this.y=y;
}
public String Show(){
return"横坐标为:"+this.x+",纵坐标为:"+this.y;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point a=new Point(3.0,4.0);
System.out.println(a.Show());
}
}
package实验三;
public class Circle extends Point {
double Radius;
public Circle(){
super(0,0);
Radius=0;
}
public Circle(double x,double y,double z){
super(x,y);
Radius=z;
}
public String Show(){
System.out.println("圆心的"+super.Show()+"半径为:"+Radius);
return"圆心的"+super.Show()+"半径为:"+Radius;
}
public String PrintArea(){
System.out.println("圆的面积为:"+Math.PI*Radius*Radius);
return"圆的面积为:"+Math.PI*Radius*Radius;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle a=new Circle(3,4,5);
a.Show();
a.PrintArea();
}
}
2.编写一测试类,对其进行编译、运行。
结果如何?如去掉语句“super.Show();”,再看看运行结果。
理解程序中重载和多态性的运用。
完成以下步骤要求:
(1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x和y值的public方法。
(2)设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r,获取和设置r值的public方法、计算圆面积的public方法。
(3)设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高
的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public 方法。
package实验三2;
public class Point {
protected double x,y;
public Point(){
x=0;
y=0;
}
public Point(double x,double y){
t his.x=x;
t his.y=y;
}
public void setx(double a){
t his.x=a;
}
public void sety(double b){
t his.y=b;
}
public double getx(){
r eturn this.x;
}
public double gety(){
r eturn this.y;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point a=new Point();
a.setx(3.0);
a.sety(4.0);
System.out.println("横坐标为:"+a.x+"纵坐标为:"+a.y);
}
}
package实验三2;
public class Circle extends Point {
protected double r;
public Circle(){
super(0,0);
r=0;
}
public void setr(double z){
t his.r=z;
}
public double getr(){
r eturn this.r;
}
public String PrintArea(){
System.out.println("圆的面积为:"+Math.PI*r*r);
return"圆的面积为:"+Math.PI*r*r;
}
public static void main(String[] args) {
// TODO自动生成的方法存根
Circle a=new Circle();
a.setx(3.0);
a.sety(4.0);
a.setr(5.0);
System.out.println("圆心坐标为:"+"("+a.getx()+","+a.gety()+")"+ "半径为:"+a.getr());
a.PrintArea();
}
}
package实验三2;
public class Cylinder extends Circle {
protected double h;
public void seth(double z){
t his.h=z;
}
public double geth(){
r eturn this.h;
}
public String Volume(){
System.out.println("圆柱体的体积为:"+Math.PI*r*r*h);
return"圆柱体的体积为:"+Math.PI*r*r*h;
}
public static void main(String[] args) {
// TODO自动生成的方法存根
Cylinder a=new Cylinder();
a.setx(3.0);
a.sety(4.0);
a.setr(5.0);
a.seth(6.0);
System.out.println("圆柱的轴心坐标为:
"+"("+a.getx()+","+a.gety()+")"+
",半径为:"+a.getr()+",高为:"+a.geth());
a.PrintArea();
a.Volume();
}
}
(4)建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值。
4.学校中有老师和学生两类人,而在职研究生既是老师又是学生,对学生的管理和对教师的管理在他们身上都有体现。
(1)设计两个信息管理接口StudentInterface和TeacherInterfaceo其中,StudentInterface接口包括setFee方法和getFee方法,分别用于设置和获取学生的学费;TeacherInterface接口包括setPay方法和getPay方法,分别用于设置和获取教师的工资。
(2)定义一个研究生类Graduate,实现StudentInterface接口和TeacherInterface 接口,它定义的成员变量有name(姓名)、sex(性别)、age(年龄)、fee(每学期学费)、pay(月工资)。
(3)创建一个姓名为"zhangsan"的研究生,统计他的年收入和学费,如果收入减去学费不足2000元,则输出“provide a loan”(需要贷款)信息。
package实验三4;
public interface StudentInterface { //创建接口
public void setFee(int tuition ); //设置学生的学费
public int getFee(); //获取学生的学费
}
package实验三4;
public interface TeacherInterface {
public void setPay(int salary); //设置教师的工资
public int getPay(); //获取教师的工资
}
package实验三4;
public class Graduate implements StudentInterface, TeacherInterface {
private String name; //研究生的姓名
private String sex; //研究生的性别
private int age; //研究生的年龄
private int fee; //研究生每学期的学费
private int pay; //研究生的月工资
public void setName(String name) { //设置姓名 = name;
}
public String getName() { //获取姓名return name;
}
public void setSex(String sex) { //设置性别this.sex = sex;
}
public String getSex() { //获取性别return sex;
}
public void setAge(int age) { //设置年龄this.age = age;
}
public int getAge() { //获取年龄return age;
}
@Override
public void setPay(int salary) {
//实现TeacherInterface接口的方法设置研究生的月工资
// TODO自动生成的方法存根
this.pay = salary;
}
@Override
public int getPay() {
//实现TeacherInterface接口的方法获取研究生的月工资
// TODO自动生成的方法存根
return this.pay;
}
@Override
public void setFee(int tuition) {
//实现StudentInterface接口的方法设置研究生的每学期学费
// TODO自动生成的方法存根
this.fee = tuition;
}
@Override
public int getFee() {
//实现StudentInterface接口的方法获取研究生的每学期学费
// TODO自动生成的方法存根
return fee;
}
public String getMessage(){
if((pay*12-2*fee)>2000){
return"这名研究生的名字是:"+name+",性别是:"+sex+"今年的学费是:"+2*fee+
",年工资是:"+12*pay+",不需要贷款!";
}
return"这名研究生的名字是:"+name+",性别是:"+sex+",今年的学费是:"+ 2*fee+",年工资是:"+12*pay+",需要贷款!";
}
public static void main(String[] args) {
// TODO自动生成的方法存根
Graduate a=new Graduate();
a.setName("zhangsan");
a.setAge(20);
a.setSex("男");
a.setFee(2000);
a.setPay(450);
System.out.println("这名研究生的名字:"+a.getName());
System.out.println("这名研究生的年龄:"+a.getAge());
System.out.println("这名研究生的性别:"+a.getSex());
System.out.println("这名研究生每学期的学费:"+a.getFee());
System.out.println("这名研究生月工资:"+a.getPay());
System.out.println(a.getMessage());
}
}。