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

时间:2015-04-20 09:09:47

标签: java polymorphism

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

  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();
        }
    }
    

4 个答案:

答案 0 :(得分:4)

  

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

这意味着

public static void print()

应该接受Shapes数组:

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();
    }
}

答案 1 :(得分:0)

您的问题是

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

答案 2 :(得分:0)

您可以在打印中定义形状,但不要填写它:

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()
   };

}

答案 3 :(得分:0)

更改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
}