You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java多态实践:基于Shape抽象类的随机异构对象数组构建作业问题

package homework.session10;
import java.util.*;

// 假设Position类已提前实现,包含接受x、y参数的构造方法
class Position {
    int x;
    int y;
    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

// 抽象父类Shape,三个图形类均继承自该类
abstract class Shape {
    protected Position pos;
    protected String fillColor;
    protected String lineColor;
    // 省略公共属性的get/set方法
}

class Triangle extends Shape {
    private double side;
    public Triangle(Position pos, String fillColor, String lineColor, double side) {
        this.pos = pos;
        this.fillColor = fillColor;
        this.lineColor = lineColor;
        this.side = side;
    }
}

class Circle extends Shape {
    private double radius;
    public Circle(Position pos, String fillColor, String lineColor, double radius) {
        this.pos = pos;
        this.fillColor = fillColor;
        this.lineColor = lineColor;
        this.radius = radius;
    }
}

class Rectangle extends Shape {
    private double width;
    private double height;
    public Rectangle(Position pos, String fillColor, String lineColor, double width, double height) {
        this.pos = pos;
        this.fillColor = fillColor;
        this.lineColor = lineColor;
        this.width = width;
        this.height = height;
    }
}

public class ShapeArray {
    // 预定义颜色池,用于随机抽取颜色值
    private static final String[] COLOR_POOL = {"红", "蓝", "绿", "黄", "黑", "白", "紫", "橙", "灰", "粉"};

    public static void main(String[] args){
        Scanner keyboard = new Scanner (System.in);
        int arraySize;
        // 校验输入的数组长度符合2-20的要求
        while (true) {
            System.out.println("请输入2到20之间的数字作为数组长度:");
            arraySize = keyboard.nextInt();
            if (arraySize >=2 && arraySize <=20) {
                break;
            }
            System.out.println("输入不符合要求,请重新输入!");
        }

        // 数组类型改为Shape,才能存储三类图形的实例
        Shape[] shapeArray = new Shape[arraySize];
        Random rnd = new Random();

        // 循环填充数组
        for (int i = 0; i < arraySize; i++) {
            // 每个实例的属性单独随机生成
            Position randomPos = new Position(rnd.nextInt(100), rnd.nextInt(100));
            String randomFillColor = COLOR_POOL[rnd.nextInt(COLOR_POOL.length)];
            String randomLineColor = COLOR_POOL[rnd.nextInt(COLOR_POOL.length)];

            // 随机生成0、1、2分别对应三类图形
            int shapeType = rnd.nextInt(3);
            switch (shapeType) {
                case 0: // 生成三角形实例
                    double triSide = 1 + rnd.nextDouble(20);
                    shapeArray[i] = new Triangle(randomPos, randomFillColor, randomLineColor, triSide);
                    break;
                case 1: // 生成圆形实例
                    double cirRadius = 1 + rnd.nextDouble(15);
                    shapeArray[i] = new Circle(randomPos, randomFillColor, randomLineColor, cirRadius);
                    break;
                case 2: // 生成矩形实例
                    double recWidth = 1 + rnd.nextDouble(20);
                    double recHeight = 1 + rnd.nextDouble(20);
                    shapeArray[i] = new Rectangle(randomPos, randomFillColor, randomLineColor, recWidth, recHeight);
                    break;
            }
        }
        System.out.println("数组填充完成,共生成" + arraySize + "个图形对象");
    }

    // 参数类型修改为Shape[],和定义的抽象父类对应
    public void sortShape(Shape[] array1){
        Arrays.sort(array1);
    }

    public void findShape(Shape[] array2 ){
        // 后续可自行实现查找逻辑
    }
}

核心修改说明

  • 数组类型修正:原代码错误声明ShapeArray[] shapeArray,改为Shape[] shapeArray,利用父类引用可指向子类对象的特性,存储三类不同图形的实例
  • 随机颜色生成:提前预定义常用颜色字符串数组,每次随机取下标获取颜色值,避免生成无意义的乱码字符串
  • 多类型实例填充:每次循环生成0~2的随机数标记图形类型,对应生成当前图形所需的随机参数后创建实例,赋值给数组对应下标位置
  • 输入校验:新增数组长度校验逻辑,确保用户输入符合2~20的作业要求

内容的提问来源于stack exchange,提问作者Galekwan Sango

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 22:24:01