1、 实验五、继承与抽象编程 1.实验目的 (1)理解抽象类的意义和实际应用的一般模式,熟练掌握抽象类和抽象方法的定义以及抽象类的继承,尤其是子类是非抽象类的情况。 2.实验内容和步骤 (1) 设计一个抽象形状类Shape,包含一个getArea()抽象方法。 (2) 在Shape类基础上设计圆形、矩形、三角形和梯形四个子类,要求根据实际形状重写getArea()方法。 abstract class Shape { protected String name; public Shape(String xm) { name = xm; System.out.p
2、rint("名称:"+name); } abstract public double getArea(); } class Circle extends Shape { private final double PI = 3.14; private double radius; public Circle(String shapeName,double r) { super(shapeName); radius = r; } public double getArea() { return PI*radius*radius; }
3、 } class Rectangle extends Shape { private double width; private double height; public Rectangle(String shapeName,double width,double height) { super(shapeName); this.width = width; this.height = height; } public double getArea() { return width*height; } } class Trape
4、zoid extends Shape { private double height; private double topline; private double baseline; public Trapezoid(String shapeName,double height,double topline,double baseline) { super(shapeName); this.height = height; this.topline = topline; this.baseline = baseline; } pub
5、lic double getArea() { return height*(topline+baseline)/2; } } class Trangle extends Shape { private double baseline; private double height; public Trangle(String shapeName,double height,double baseline) { super(shapeName); this.height = height; this.baseline = baseline;
6、 } public double getArea() { return height*baseline/2; } } public class AB { public static void main(String[] args) { Shape circle = new Circle("圆",5.0); System.out.print(";面积="+circle.getArea()); System.out.print("\n"); Shape rect= new Rectangle("矩形",3.0,5.0); System.
7、out.print(";面积="+rect.getArea()); System.out.print("\n"); Shape trangle = new Trangle("三角形",4.2,5.5); System.out.print(";面积="+trangle.getArea()); System.out.print("\n"); Shape trapezoid =new Trapezoid("梯形",5.0,4.0,9.0); System.out.print(";面积="+trapezoid.getArea()); System.out.pr
8、int("\n"); } } (3) 设计一个TestShape类,包含变量area(存储总面积)、静态方法countArea(Shape s),该方法负责把参数中的形状面积加入到area中。在main函数中新建(2)中四种类型的对象s1、s2、s3、s4,通过调用countArea方法把四个对象面积累加到area中,最后输出area。 (4) 将形状类Shape改为接口,在不修改测试类TestShape的基础上,保证执行TestShape能够得到与实验四同样的结果。 interface IShape { final double PI = 3.14; abstrac
9、t public double getArea(); } class Circle implements IShape { double radius; public Circle(double r) { radius = r; } public double getArea() { return PI*radius*radius; } } class Rectangle implements IShape { private double width; private double height; public Rectangl
10、e(double width,double height) { this.width = width; this.height = height; } public double getArea() { return width*height; } } class Trapezoid implements IShape { private double height; private double topline; private double baseline; public Trapezoid(double height,doub
11、le topline,double baseline) { this.height = height; this.topline = topline; this.baseline = baseline; } public double getArea() { return height*(topline+baseline)/2; } } class Trangle implements IShape { private double baseline; private double height; public Trangle(do
12、uble height,double baseline) { this.height = height; this.baseline = baseline; } public double getArea() { return height*baseline/2; } } public class ABCD { public static void main(String[] args) { IShape circle = new Circle(3.0); System.out.print("圆面积="+circle.getAre
13、a()); System.out.print("\n"); Rectangle rect = new Rectangle(2.5,3.2); System.out.print("矩形面积="+rect.getArea()); System.out.print("\n"); Trapezoid trapezoid =new Trapezoid(5.0,6.0,9.0); System.out.print("梯形面积="+trapezoid.getArea()); System.out.print("\n"); Trangle trangle = new Trangle(6.0,5.0); System.out.print("三角形面积="+trangle.getArea()); System.out.print("\n"); } }
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818