java设计图形(Shape)类及其子类(Circle、Rectangle)
发布网友
发布时间:2022-05-27 10:06
我来回答
共1个回答
热心网友
时间:2023-10-16 08:34
你好,刚好闲着帮你写一个:
Shape类:
public class Shape {
protected Point location;
public Shape(){
}
public double area(){
return 0.0;
}
}
Circle类:
public class Circle extends Shape{
private int r;
public Circle() {
}
public Circle(Point center,int r) {
super.location=center;
this.r = r;
}
public double area() {
return Math.PI*r*r ;
}
}
Rectangle类:
public class Rectangle extends Shape{
private int width;
private int height;
public Rectangle() {
}
public Rectangle(Point o,int width, int height) {
location=o;
this.width = width;
this.height = height;
}
public double area() {
return width*height;
}
}
我这里图方便,在创建圆的时候直接用圆心和半径创建,还有矩形也是用一个点位置和长宽创建,所以还要加一个点类:
public class Point {
public int x;
public int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}