Java中的继承练习

时间:2016-07-03 07:32:16

标签: java inheritance

我已经用Java编写了一个继承程序; Box和Line是Shape的子类,draw()是Shape.in的函数.ReginPaint类我也编写了menu()和display()函数。当我想单独添加行或添加框时,结果很好,但每当我添加行并在添加框之后,结果都是错误的。我认为问题在于我对Shape数组的错误定义以及我使用indexShapeBox和indexShapeLine的方式。我的代码在这里:

    package inherpaint;

    import java.util.Scanner;

public class InherPaint {

Scanner key = new Scanner(System.in);
Shape[] listShape = new Shape[80];
int indexShapeBox = 0;
int indexShapeLine = 0;

public static void main(String[] args) {

    InherPaint p = new InherPaint();
    p.menu();
}

void display() {

    System.out.println(" *** Paint System ***");
    System.out.println("1.Add box");
    System.out.println("2.Add line");
    System.out.println("3.Show all");
    System.out.println("4.Erase all");
    System.out.println("5.Exit");

}

void menu() {

    int option = 0;
    while (option != 5) {

        display();
        option = key.nextInt();
        switch (option) {

            case 1:
                System.out.println(" Please enter a nomber for sizeX : ");
                int X = key.nextInt();
                System.out.println(" Please enter a nomber for sizeY : ");
                int Y = key.nextInt();
                Box b = new Box(X, Y);
                listShape[indexShapeBox] = b; // problem must be here !
                indexShapeBox++;

                break;

            case 2:
                System.out.println(" Please enter a nomber for sizeZ : ");
                int Z = key.nextInt();
                Line l = new Line(Z);
                listShape[indexShapeLine] = l; // problem must be here !
                indexShapeLine++;

                break;

            case 3:
                for (int i = 0; i < indexShapeBox; i++) {
                    listShape[i].draw();
                }

                for (int i = 0; i < indexShapeLine; i++) {
                    listShape[i].draw();
                }

                break;

            case 4:
                indexShapeBox = 0;
                indexShapeLine = 0;

                break;

            case 5:
                System.out.println("Exit!");

                break;

            default:
                System.out.println("Error");

            }

        }

    }

 }

这是Shape类:

    package inherpaint;

    public abstract class Shape {

         int sizeX,sizeY,sizeZ;
         public abstract void draw();

 }

这是Box类:

    package inherpaint;

    public class Box extends Shape {

      public Box(int sizeX, int sizeY) {
         this.sizeX = sizeX;
         this.sizeY = sizeY;
}

    @Override
    public void draw() {

        for (int i = 0; i < sizeX; i++) {

            for (int j = 0; j < sizeY; j++) {
                System.out.print("-");

            }

            System.out.println();
        }
    }

}

这是Line class:

    package inherpaint;

    public class Line extends Shape {

      public Line(int sizeZ) {
      this.sizeZ = sizeZ;
}


    @Override
    public void draw() {

        for (int j = 0; j < sizeZ; j++) {
            System.out.print("-");

        }
    }
}

有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

您有一个单独的行和框计数器,即使您将它们存储在同一个数组中。这是你的问题。你只需要一个柜台。

考虑以下代码:

        case 1:
            ...
            listShape[indexShapeBox] = b;
            indexShapeBox++;
            ...
        case 2:
            ...
            listShape[indexShapeLine] = l;
            indexShapeLine++;

如果添加一个框然后添加一行,它们将被添加到listShape数组的相同索引中,因此行引用将覆盖框引用。

将您的代码更改为:

        case 1:
            ...
            listShape[indexShape] = b;
            indexShape++;
            ...
        case 2:
            ...
            listShape[indexShape] = l;
            indexShape++;

您的绘图循环也应该简化:

        case 3:
            for (int i = 0; i < indexShape; i++) {
                listShape[i].draw();
            }