参照类图编写类代码; (2)定义图形抽象类,定义抽象draw方法,并为具体图形类定义具体方法的实 现; (3)设计并定义具体图形面积计算、周长计算方法; (4)编写Test类,测试draw方法
时间: 2024-05-14 11:13:58 浏览: 93
1. 类代码
```
abstract class Shape {
public abstract void draw();
public abstract double getArea();
public abstract double getPerimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public void draw() {
System.out.println("Drawing a circle");
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public void draw() {
System.out.println("Drawing a rectangle");
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
public void draw() {
System.out.println("Drawing a square");
}
public double getArea() {
return side * side;
}
public double getPerimeter() {
return 4 * side;
}
}
```
2. 图形抽象类和具体方法的实现
```
abstract class Shape {
public abstract void draw();
public abstract double getArea();
public abstract double getPerimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public void draw() {
System.out.println("Drawing a circle");
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public void draw() {
System.out.println("Drawing a rectangle");
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
public void draw() {
System.out.println("Drawing a square");
}
public double getArea() {
return side * side;
}
public double getPerimeter() {
return 4 * side;
}
}
```
3. 具体图形面积计算、周长计算方法
在上面的类中已经定义了具体图形面积计算、周长计算方法,分别是getArea()和getPerimeter()方法。
4. Test类
```
public class Test {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(3, 4);
Shape square = new Square(2);
circle.draw();
System.out.println("Area of circle: " + circle.getArea());
System.out.println("Perimeter of circle: " + circle.getPerimeter());
rectangle.draw();
System.out.println("Area of rectangle: " + rectangle.getArea());
System.out.println("Perimeter of rectangle: " + rectangle.getPerimeter());
square.draw();
System.out.println("Area of square: " + square.getArea());
System.out.println("Perimeter of square: " + square.getPerimeter());
}
}
```
输出结果:
```
Drawing a circle
Area of circle: 78.53981633974483
Perimeter of circle: 31.41592653589793
Drawing a rectangle
Area of rectangle: 12.0
Perimeter of rectangle: 14.0
Drawing a square
Area of square: 4.0
Perimeter of square: 8.0
```
阅读全文