类的继承
实验目的:掌握类的继承、抽象类的定义和使用、对象的向上转型。
实验内容:已知若干图形,求以该图形为底的锥体的体积。
实验要求:用面向抽象的思想实现。
实验步骤:
1、程序总体框架
2.在shape包中创建抽象类Shape,代码如下:
package shape;
public abstract class Shape {
public abstract double getArea(); //建立一个抽象类求Shape面积
}
3、在centrun包中创建一个类Centrun,代码如下:
package centrun;
import shape.Shape;
public class Centrun {
short S1=1;
float F1=(float) 3.4;
double height;
Shape shape;
//此处未实例化对象,知识点:对象的向上转型
public Centrun(){
super();
}
public Centrun(double height,Shape shape) {
super();
this.height=height;
this.shape=shape;
}
public double getVolume(){
return (1.0/3)*shape.getArea()*this.height;
//求椎体的体积,getArea将圆的面积算法单独作为一个类(Circle.java),便于代码的复用&