java实现一个直线类继承于图形抽象类,属性有:起点和终点,功能是计算长度
时间: 2024-05-27 17:14:23 浏览: 40
设计一个抽象类图形类,在该类中包含有至少两个抽象方法求周长和求面积,分别定义圆形类、长方形类、正方形类、三角形类来继承图形类,并实现上述两个方法
5星 · 资源好评率100%
public abstract class Shape {
public abstract double calculateArea();
public abstract double calculatePerimeter();
}
public class Line extends Shape {
private Point startPoint;
private Point endPoint;
public Line(Point startPoint, Point endPoint) {
this.startPoint = startPoint;
this.endPoint = endPoint;
}
public Point getStartPoint() {
return startPoint;
}
public Point getEndPoint() {
return endPoint;
}
public void setStartPoint(Point startPoint) {
this.startPoint = startPoint;
}
public void setEndPoint(Point endPoint) {
this.endPoint = endPoint;
}
@Override
public double calculateArea() {
return 0;
}
@Override
public double calculatePerimeter() {
return startPoint.distance(endPoint);
}
}
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double distance(Point otherPoint) {
double dx = x - otherPoint.getX();
double dy = y - otherPoint.getY();
return Math.sqrt(dx * dx + dy * dy);
}
}
阅读全文