# javaee-test-oop **Repository Path**: ChalkyCrystal/javaee-test-oop ## Basic Information - **Project Name**: javaee-test-oop - **Description**: OOP回顾 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 205 - **Created**: 2026-03-09 - **Last Updated**: 2026-03-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 面向对象程序设计回顾 ## 一、类的定义与使用 ### 1. 声明Point类 1) 添加两个整型属性`x`,`y` 2) 提供构造函数`Point()`,`Point(int x,int y)`和`Point(Point p)` ```java public class Point { private int x; private int y; public Point() { this.x = 0; this.y = 0; } public Point(int x, int y) { this.x = x; this.y = y; } public Point(Point p) { this.x = p.x; this.y = p.y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } ``` ### 2. 声明Rectangle类 1) 添加四个属性:`Point ptTopLeft`, `Point ptTopRight`, `int width`,`int height` 2) 为Rectangle类提供构造函数`Rectangle()`,`Rectangle(Point topleft,int hight,int width)` 3) 为Rectangle类添加并实现`double perimeter()`,`double area()`,`void draw()`方法 ```java public class Rectangle implements IShape { private Point ptTopLeft; private Point ptTopRight; private int width; private int height; public Rectangle() { this.ptTopLeft = new Point(); this.ptTopRight = new Point(); this.width = 0; this.height = 0; } public Rectangle(Point topleft, int height, int width) { this.ptTopLeft = new Point(topleft); this.height = height; this.width = width; this.ptTopRight = new Point(topleft.getX() + width, topleft.getY()); } @Override public double perimeter() { return 2 * (width + height); } @Override public double area() { return width * height; } @Override public void draw() { System.out.println("绘制矩形:"); System.out.println("左上角点:" + ptTopLeft); System.out.println("右上角点:" + ptTopRight); System.out.println("宽度:" + width + ",高度:" + height); System.out.println("周长:" + perimeter() + ",面积:" + area()); System.out.println("------------------------"); } } ``` ### 3. 测试Rectangle类 ## 二、 类与接口 ### 1. 声明IShape接口,添加三个接口方法`double perimeter()`,`double area()`,`void draw()` ```java public interface IShape { double perimeter(); double area(); void draw(); } ``` ### 2. 声明Circle类实现IShape接口 1) 添加两个属性:`Point ptCenter`,`int radius` 2) 提供构造函数`Circle(Point center,int r)` 3) 实现IShape的三个接口方法`double perimeter()`,`double area()`,`void draw()` ```java public class Circle implements IShape { private Point ptCenter; private int radius; public Circle(Point center, int r) { this.ptCenter = new Point(center); this.radius = r; } @Override public double perimeter() { return 2 * Math.PI * radius; } @Override public double area() { return Math.PI * radius * radius; } @Override public void draw() { System.out.println("绘制圆形:"); System.out.println("圆心:" + ptCenter); System.out.println("半径:" + radius); System.out.println("周长:" + String.format("%.2f", perimeter()) + ",面积:" + String.format("%.2f", area())); System.out.println("------------------------"); } } ``` ### 3. 声明Triangle类实现IShape接口 1) 添加三个属性:`Point ptA`,`Point ptB`,`Point ptC` 2) 添加三个只读属性:`double edgeA`,`double edgeB`,`double edgeC` 3) 提供构造函数`Triangle(Point ptA,Point ptB,Point ptC)` 4) 实现IShape的三个接口方法`double perimeter()`,`double area()`,`void draw()` ```java public class Triangle implements IShape { private Point ptA; private Point ptB; private Point ptC; private double edgeA; // BC边 private double edgeB; // AC边 private double edgeC; // AB边 public Triangle(Point ptA, Point ptB, Point ptC) { this.ptA = new Point(ptA); this.ptB = new Point(ptB); this.ptC = new Point(ptC); calculateEdges(); } private void calculateEdges() { // 两点间距离公式:√[(x2-x1)² + (y2-y1)²] this.edgeA = Math.sqrt(Math.pow(ptC.getX() - ptB.getX(), 2) + Math.pow(ptC.getY() - ptB.getY(), 2)); this.edgeB = Math.sqrt(Math.pow(ptC.getX() - ptA.getX(), 2) + Math.pow(ptC.getY() - ptA.getY(), 2)); this.edgeC = Math.sqrt(Math.pow(ptB.getX() - ptA.getX(), 2) + Math.pow(ptB.getY() - ptA.getY(), 2)); } public double getEdgeA() { return edgeA; } public double getEdgeB() { return edgeB; } public double getEdgeC() { return edgeC; } @Override public double perimeter() { return edgeA + edgeB + edgeC; } @Override public double area() { double s = perimeter() / 2; return Math.sqrt(s * (s - edgeA) * (s - edgeB) * (s - edgeC)); } @Override public void draw() { System.out.println("绘制三角形:"); System.out.println("顶点A:" + ptA + ",顶点B:" + ptB + ",顶点C:" + ptC); System.out.println("边长A:" + String.format("%.2f", edgeA) + ",边长B:" + String.format("%.2f", edgeB) + ",边长C:" + String.format("%.2f", edgeC)); System.out.println("周长:" + String.format("%.2f", perimeter()) + ",面积:" + String.format("%.2f", area())); System.out.println("------------------------"); } } ``` ### 4. 改造一中的Rectangle类,使之实现IShape接口 ### 5. 声明ShapeManager类 1) 添加只读属性`List shapes` 2) 添加`addShape(IShape shape)`方法,实现将参数shape对象添加到shapes列表中 3) 添加`delShape(IShape shape)`方法,实现将参数shape对象从shapes列表中删除 4) 添加`drawShapes()`方法,实现shapes列表中所有shape元素的绘制 ```java import java.util.ArrayList; import java.util.List; public class ShapeManager { private final List shapes; public ShapeManager() { this.shapes = new ArrayList<>(); } public List getShapes() { return new ArrayList<>(shapes); } public void addShape(IShape shape) { if (shape != null && !shapes.contains(shape)) { shapes.add(shape); System.out.println("添加形状成功!"); } else { System.out.println("形状为空或已存在,添加失败!"); } } public void delShape(IShape shape) { if (shapes.contains(shape)) { shapes.remove(shape); System.out.println("删除形状成功!"); } else { System.out.println("形状不存在,删除失败!"); } } public void drawShapes() { System.out.println("\n===== 开始绘制所有形状 ====="); if (shapes.isEmpty()) { System.out.println("暂无形状可绘制!"); } else { for (IShape shape : shapes) { shape.draw(); } } System.out.println("===== 绘制完成 =====\n"); } } ``` ### 6. 测试代码 ```java public static void Main(String[] args){ ShapeManager shapeManager=new ShapeManager(); Rectangle rect=new Rectangle(new Point(10,10),100,200); Triangle tri=new Triangle(new Point(0,10),new Point(50,50),new Point(150,0)); Circle cir=new Circle(new Point(100,100),100); shapeManager.addShape(rect); shapeManager.addShape(tri); shapeManager.addShape(cir); shapeManager.drawShapes(); shapeManager.delShape(rect); shapeManager.drawShapes(); } ```