java 面积编程题
发布网友
发布时间:2023-07-04 08:58
我来回答
共2个回答
热心网友
时间:2023-10-10 16:11
public class Test
{
public static void main(String[] args)
{
Circle c = new Circle("black",5.2);
Rectangle r = new Rectangle("white",2.3,3.5);
System.out.println(c.show());
System.out.println(r.show());
}
}
abstract class Shape
{
String color;
Shape()
{}
Shape(String color)
{
this.color = color;
}
abstract public double getArea();
}
class Circle extends Shape
{
private double r;
Circle(String color,double r)
{
this.color = color;
this.r = r;
}
public double getArea()
{
return Math.PI*Math.pow(r,2);
}
public String show()
{
return "r:" + r + ", color:" + color;
}
}
class Rectangle extends Shape
{
private double a,b;
Rectangle(String color,double a,double b)
{
this.color = color;
this.a = a;
this.b = b;
}
public double getArea()
{
return a*b;
}
public String show()
{
return "a:" + a + " b:" + b +", color:" + color;
}
}
热心网友
时间:2023-10-10 16:12
自己写 思路都这么清楚了还不动手写 要是概念模糊还有人乐意提点你 像这样具体的题纯粹就是手懒