这段代码有什么问题?(多态性)



我试图修复代码,但我不知道如何修复它。

  1. print方法中,编写一个 for 循环以遍历数组形状中的每个元素并调用该方法 draw

  2. 在方法TestCase2中,调用上面的方法print并将数组形状传递给此方法。

    package Polymorphism.test;
    import Polymorphism.*;
    
    public class TestShape {
    
        public static void main(String[] args) {
        int testCase = 1;
        switch (testCase)
        {
            case 1:
                TestCase1();
                break;
            case 2:
                TestCase2();
                break;
            default:
                System.out.println("Invalid test case selection!");
        }
        }
        public static void print()
        {
            Shape[] shapes;
            System.out.println("Test an array of Shape:");
            for(int i = 0; i < shapes.length; i++)
            {
                shapes[i].draw();
            }
        }
        public static void TestCase1()
        {
            Shape shape;
            shape = new Shape();
            shape.draw();
            shape = new Polygon();
            shape.draw();
            shape = new Rectangle();
            shape.draw();
            shape = new Square();
            shape.draw();
            shape = new Circle();
            shape.draw();
        }
        public static void TestCase2()
        {
            Shape[] shapes = {new Shape(), new Polygon(), new Rectangle(), new  Square(), new Circle(), new Polygon(), new Rectangle()};
            shapes.print();
        }
    }
    

在 TestCase2 方法中,调用上面的方法打印并将数组形状传递给此方法

这意味着

public static void print()

应该接受形状数组:

public static void print(Shape[] shapes)

你应该把这个数组传递给它,所以改变:

shapes.print();

TestShape.print(shapes);

您的打印方法将变为:

public static void print(Shape[] shapes)
{
    System.out.println("Test an array of Shape:");
    for(int i = 0; i < shapes.length; i++)
    {
        shapes[i].draw();
    }
}

您的问题在于

public static void print()
{
    Shape[] shapes;
    System.out.println("Test an array of Shape:");
    for(int i = 0; i < shapes.length; i++)
    {
        shapes[i].draw();
    }
}

您需要初始化形状数组,否则将得到NullPointerException

更改print()如下:

public static void print(Shape [] shapes)   // HERE PASS shapes AS ARGUMENT
{
    System.out.println("Test an array of Shape:");
    for(int i = 0; i < shapes.length; i++)
        {
            shapes[i].draw();
        }
    }
}

TestCase2()如下:

public static void TestCase2()
{
    Shape[] shapes = {
        new Shape(), new Polygon(), new Rectangle(),
        new  Square(), new Circle(), new Polygon(),
        new Rectangle()
    };
    print(shapes); // HERE CALL STATIC METHOD
}

您在打印中定义形状,但不填充它:

public static void print()
{
    Shape[] shapes;
    System.out.println("Test an array of Shape:");
    // shapes is empty always!!!!
    for(int i = 0; i < shapes.length; i++)
    {
        shapes[i].draw();
    }
}

将 Shape[] 形状定义为类的属性,以便在任何地方使用它:

package Polymorphism.test;
import Polymorphism.*;

public class TestShape {
// declare field!!!
Shape[] shapes;

public static void main(String[] args) {
int testCase = 1;
switch (testCase)
{
    case 1:
        TestCase1();
        break;
    case 2:
        TestCase2();
        break;
    default:
        System.out.println("Invalid test case selection!");

    // execute the print function
    print();
}
}
public static void print()
{
    System.out.println("Test an array of Shape:");
    for(int i = 0; i < shapes.length; i++)
    {
        shapes[i].draw();
    }
}
public static void TestCase1()
{
    shapes = new Shape[5];
    shape = new Shape();
    shapes[0] = shape;
    shape = new Polygon();
    shapes[1] = shape;
    shape = new Rectangle();
    shapes[2] = shape;
    shape = new Square();
    shapes[3] = shape;
    shape = new Circle();
    shapes[4] = shape;
}
public static void TestCase2()
{
    Shape[] shapes = {
         new Shape(), new Polygon(), new Rectangle(), 
         new  Square(), new Circle(), new Polygon(),
         new Rectangle()
   };
}

最新更新