展开全部
package com.ZL;
public abstract class Shape {
public abstract double getArea(Shape extends T> a);
}
package com.ZL;
public class Circle extends Shape{
private double radius;
public Circle(double radius) {
e69da5e6ba9062616964757a686964616f31333431373839super();
this.radius = radius;
}
@Override
public double getArea(Shape a) {
Circle c = (Circle)a;
double r = c.radius;
return (Math.PI)*(Math.pow(r, 2));
}
}
package com.ZL;
public class Rectangle extends Shape{
private double length;
private double width;
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
@Override
public double getArea(Shape a) {
Rectangle r = (Rectangle)a;
return r.length * r.width;
}
}
package com.ZL;
public class Triangle extends Shape{
private double length;
private double heigth;
public Triangle(double length, double heigth) {
super();
this.length = length;
this.heigth = heigth;
}
@Override
public double getArea(Shape a) {
Triangle t = (Triangle)a;
return (t.length * t.heigth) / 2;
}
}
package com.ZL;
public class Trapezoid extends Shape{
private double upperLength;
private double bottonLength;
private double heigth;
public Trapezoid(double upperLength, double bottonLength, double heigth) {
super();
this.upperLength = upperLength;
this.bottonLength = bottonLength;
this.heigth = heigth;
}
@Override
public double getArea(Shape a) {
Trapezoid t = (Trapezoid)a;
return (t.upperLength + t.bottonLength) / 2 * t.heigth;
}
}
package com.ZL;
public class TestShape {
public static void main(String[] args) {
Shape circle = new Circle(5.3);
Shape rectangle = new Rectangle(3.0,6.6);
Shape triangle = new Triangle(8.0,5.0);
Shape trapezoid = new Trapezoid(4.0,7.2,12.0);
System.out.println(circle.getArea(circle));
System.out.println(rectangle.getArea(rectangle));
System.out.println(triangle.getArea(triangle));
System.out.println(trapezoid.getArea(trapezoid));
}
}
输出结果
88.24733763933729
19.799999999999997
20.0
67.19999999999999
这要是不采纳就说不过去了哈🧐
更多追问追答
追答
尖括号里的代码被吞了,直接看图吧
已赞过
已踩过<
你对这个回答的评价是?
评论
收起