# javaee-test-oop **Repository Path**: suruixin123/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**: 159 - **Created**: 2025-03-01 - **Last Updated**: 2025-03-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 面向对象程序设计回顾 ## 一、类的定义与使用 ### 1. 声明Point类 public class Point { public int x; public int y; public Point() { } 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类 public class Rectangle{ private Point ptTopLeft; private Point ptTopRight; private int width; private int height; public Rectangle() { } public Rectangle(Point topLeft, int height, int width) { this.ptTopLeft = topLeft; this.ptTopRight = new Point(topLeft.getX() + width, topLeft.getY()); this.height = height; this.width = width; } public double perimeter() { return 2 * (this.height + this.width); } public double area() { return this.height * this.width; } public void draw() { System.out.println("绘制宽为:"+width+" 高为:"+height+"的矩形"); } } ### 3. 测试Rectangle类 ## 二、 类与接口 ### 1. 声明IShape接口,添加三个接口方法 public interface IShape { double perimeter(); double area(); void draw(); } ### 2. 声明Circle类实现IShape接口 public class Circle implements IShape{ private Point ptCenter; private int r; public Circle(Point center, int r) { this.ptCenter = center; this.r = r; } @Override public double perimeter() { return 2*Math.PI*r; } @Override public double area() { return Math.PI*r*r; } @Override public void draw() { System.out.println("以圆心坐标x:"+ptCenter.getX()+" y:"+ptCenter.getY()+" 半径为"+r+"绘制圆"); } } ### 3. 声明Triangle类实现IShape接口 public class Triangle implements IShape{ private Point ptA; private Point ptB; private Point ptC; private double edgeA; private double edgeB; private double edgeC; public Triangle(Point ptA, Point ptB, Point ptC) { this.ptA = ptA; this.ptB = ptB; this.ptC = ptC; } @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("以坐标点A、B、C: (" + ptA.getX() + ", " + ptA.getY() + "), (" + ptB.getX() + ", " + ptB.getY() + "), (" + ptC.getX() + ", " + ptC.getY() + ")绘制三角形"); } } ### 4. 改造一中的Rectangle类,使之实现IShape接口 public class Rectangle implements IShape{ private Point ptTopLeft; private Point ptTopRight; private int width; private int height; public Rectangle() { } public Rectangle(Point topLeft, int height, int width) { this.ptTopLeft = topLeft; this.ptTopRight = new Point(topLeft.getX() + width, topLeft.getY()); this.height = height; this.width = width; } @Override public double perimeter() { return 2 * (this.height + this.width); } @Override public double area() { return this.height * this.width; } @Override public void draw() { System.out.println("绘制宽为:"+width+" 高为:"+height+"的矩形"); } } ### 5. 声明ShapeManager类 import java.util.ArrayList; import java.util.List; public class ShapeManager { private List shapes=new ArrayList<>(); public void addShape(IShape shape){ shapes.add(shape); } public void delShape(IShape shape){ shapes.remove(shape); } public void drawShapes(){ for(IShape shape : shapes){ shape.draw(); } } } ### 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(); } ```