实验五 继承与多态
理解继承与多态的概念,掌握三种访问控制的区别,掌握抽象类、接口与包的应用。
二、实验内容:
1. 参考教材P112,示例程序C5_17,掌握抽象类的应用。
2. 参考教材P116,示例程序C5_19,掌握接口的使用方法。
3. 参考教材P121,示例程序C5_20,掌握包的使用方法。
三、实验要求:
1.完成示例程序的验证。
2.写出实验报告。
四、实验步骤与结果:(由学生按照实验内容完成)
1. 示例程序c5_17
import java.awt.*;
import java.applet. *;
abstract class Shapes //定义一个抽象类Shapes
{
public int x,y; //x、y为画图的坐标
public int width,height;
public Shapes(int x,int y,int width,int height)
{ this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
abstract double getArea( ); //求图形面积的抽象方法
abstract double getPerimeter( ); //求图形周长的抽象方法
}
class Square extends Shapes //由抽象类Shapes派生的子类——矩形类 {
public double getArea( ){return(width*height);}
public double getPerimeter( ){return(2*width+2*height);}
public Square(int x,int y,int width,int height)
{ super(x,y,width,height); }
}