-1

所以我有这个代码:

https://gist.github.com/anonymous/0760d154f81064bf8493(抱歉这是 5 个课程,所以我不能放在这里)

而且我认为我已经在相当程度上实现了多态性。但是我还能做些什么来让它更加多态吗?

另外,在主类中,我制作了这样的局部变量(shape,shape1,shape2,area,area1,area2),我觉得由于继承或其他原因,我不应该这样做。我不确定它似乎不正确。

谢谢

4

4 回答 4

1

多态不是解决问题的工具,如果你愿意,它是一种范式。

也就是说,问题应该是:什么需要更多的多态性?你的改变有什么收获吗?有什么要求吗?您尝试归档的目标是什么?

回答这些问题将告诉您对代码进行哪些更改,如果它需要任何更改的话。

于 2013-10-23T13:44:51.760 回答
1

我会这样改变 MyShapes

import java.util.Scanner;


public class MyShapes 
{
    public static Scanner scan = new Scanner(System.in);

    public static int Menu()
    {
        System.out.println("\nSelect a Shape or Exit: \n");
        System.out.println("1. Square");
        System.out.println("2. Circle");
        System.out.println("3. Triangle");
        System.out.println("4. Exit");

        System.out.println("\nEnter choice:");
        int option = scan.nextInt();

        return option;
    }// end menu

    public static void main(String[] args) 
    {       
        int option = 0;

        while (option != 4)
        {
            option = Menu();

            Shape shape = null; 

            switch(option)
            {
                case 1:
                    shape = new Circle(scan.nextDouble());
                    break;

                case 2:
                    Shape shape = new Square(scan.nextDouble());
                    break;
                case 3:
                    shape = new Triangle(scan.nextDouble());
                    break;

                case 4:
                    System.out.println("System closing");
                    System.out.println("-----------\n");
                    //System.exit(0);   <-- not needed
                    break;
                default:
                    System.out.println("Invalid option");
                    System.out.println("--------------\n");
            }
            printShape(shape); //check null needed.
        }
    }

    private void printShape(Shape shape){
        if(shape != null){
            double boundaryLength1 = shape.getBoundaryLength();
            double area1 = shape.getArea();
            System.out.println("Boundary Length = " + Math.round(boundaryLength1));
            System.out.println("Area = " + Math.round(area1));
        }
    }
}

一个更好的选择是将打印放在每个对象中,而不是 MyShapes 类。

另一件事, System.exit() 不是一个好习惯,如果选择了 Exit 选项,您的代码将结束,而无需 System.exit() 它,只需删除该行。


工厂

比继承更高级的主题是设计模式,当根据 a 创建对象时,parameter最好使用工厂。

public class ShapeFactory{

    public static Shape createShape(int option){
        //Your switch here.
    }
}

并且您的 MyShapes 课程将更改为

import java.util.Scanner;


public class MyShapes 
{
    public static Scanner scan = new Scanner(System.in);

    public static int Menu()
    {
        System.out.println("\nSelect a Shape or Exit: \n");
        System.out.println("1. Square");
        System.out.println("2. Circle");
        System.out.println("3. Triangle");
        System.out.println("4. Exit");

        System.out.println("\nEnter choice:");
        int option = scan.nextInt();

        return option;
    }// end menu

    public static void main(String[] args) 
    {       
        int option = 0;

        while (option != 4)
        {
            option = Menu();

            Shape shape = ShapeFactory.createShape(option); 
            printShape(shape); //check null needed.
        }
    }

    private void printShape(Shape shape){
        if(shape != null){
            double boundaryLength1 = shape.getBoundaryLength();
            double area1 = shape.getArea();
            System.out.println("Boundary Length = " + Math.round(boundaryLength1));
            System.out.println("Area = " + Math.round(area1));
        }
    }
}
于 2013-10-23T13:47:32.440 回答
1

多态性本身没有任何问题。不过,您构建形状的方式有点奇怪。

这会更有意义:

if (option==CLOSE_OPTION) end();

Shape shape = getShape(option);
if (shape!=null) {
    double boundaryLength = shape.getBoundaryLength();
    double area = shape.getArea();
    System.out.println("Boundary Length = " + Math.round(boundaryLength));
    System.out.println("Area = " + Math.round(area));
}

...

private Shape getShape(int option){
    switch(option) {
        case 1: 
            return new Circle(scan.nextDouble());
        case 2:
            return new Square(scan.nextDouble());
        case 3:
            return new Triangle(scan.nextDouble());
        default:
            System.out.println("Invalid option");
            System.out.println("--------------\n");
            return null;
    }
}
于 2013-10-23T13:50:44.690 回答
1

你所做的并没有什么问题。Shape1/Shape2 等只是您用来存储“Shape”实例的私有变量,这很正常......

我猜这是一个家庭作业练习,你已经证明你理解超类中的重写方法,但是如果你想全力以赴,你可以让“形状”成为一个带有通用方法定义的接口,然后方形/三角形等会必须实现 Shape 接口。然后,当您在“MyShapes”中实例化它们时,您会像这样创建它们:

Shape t = new Triangle();

这样你就说“t是一个实现 Shape 接口的对象的实例,我真的不需要关心到底t是什么,因为我知道它履行了 Shape 的合同”

于 2013-10-23T13:53:10.790 回答