package b; public class Rect { Double width; Double height; public Double getWidth() { return width; } public void setWidth(Double width) { this.width = width; } public Double getHeight() { return height; } public void setHeight(Double height) { this.height = height; } public Rect(Double width, Double height) { super(); this.width = width; this.height = height; } Rect() { width=10.0; height=10.0; } public void area() { if(width<0||height<0) { System.out.println("数据错误"); } else System.out.println("矩形的面积为 width*height="+(width*height)); } public void perimeter() { if(width<0||height<0) { System.out.println("数据错误"); } else System.out.println("矩形的周长为 2*(width+height)="+(2*(width+height))); } }
public class PlainRect extends Rect { private double startX; private double startY; public double getStartX() { return startX; } public void setStartX(double startX) { this.startX = startX; } public double getStartY() { return startY; } public void setStartY(double startY) { this.startY = startY; } public PlainRect(Double width, Double height, double startX, double startY) { super(width, height); this.startX = startX; this.startY = startY; } PlainRect() { width=0.0; height=0.0; startX=0.0; startY=0.0; } boolean isInside(double x,double y) { if(x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)) { System.out.println("("+x+","+y+")在矩形内"); return true; } else { System.out.println("("+x+","+y+")不在矩形内"); return false; } } }
package b; public class TestRect { public static void main(String[] args) { PlainRect juxing=new PlainRect(); juxing.setWidth(10.0); juxing.setHeight(20.0); juxing.area(); juxing.perimeter(); juxing.setStartX(10); juxing.setStartY(10); juxing.isInside(25.5, 13); } }
时间: 2024-12-28 02:44:20