实验五 继承与多态
专业班级
学 号
姓 名
实验学时
2
实验类型
验证性
实验地点
软件工程实验室
实验时间
2013-11-21
指导老师
曹记东
实验成绩
年 月 日
一、实验目的:
理解继承与多态的概念,掌握三种访问控制的区别,掌握抽象类、接口与包的应用。
二、实验内容:
1. 参考教材P112,示例程序C5_17,掌握抽象类的应用。
2. 参考教材P116,示例程序C5_19,掌握接口的使用方法。
3. 参考教材P121,示例程序C5_20,掌握包的使用方法。
三、实验要求:
。
。
四、实验步骤与结果:(由学生按照实验内容完成)
1. 示例程序c5_17
import .*;
import . *;
abstract class Shapes //定义一个抽象类Shapes
{
public int x,y; //x、y为画图的坐标
public int width,height;
public Shapes(int x,int y,int width,int height)
{ =x;
=y;
=width;
=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); }
}
class Triangle extends Shapes //由抽象类Shapes派生的子类——三角形类
{
public double c; //斜边
public double getArea( ){return(*width*height);}
public double getPerimeter( ){return(width+height+c);}
public Triangle(int x,int y,int base,int height)
{
super(x,y,base,height);
c=(width*width+height*height);
}
}
继承与多态实验报告 来自淘豆网www.taodocs.com转载请标明出处.