java实现一个直线类继承于图形抽象类,属性有:起点和终点,功能是计算长度
时间: 2024-05-27 07:14:23 浏览: 39
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);
}
}
阅读全文