0

到目前为止,我已经完成了此代码,但我正在尝试学习如何完成此方法,以便它将编译使用的总边数并在最后一个打印行中打印该值。问题方法是getTotalSides,我目前将它设置为返回0,但我希望它返回总边数,意思是:mp1.getSides() + mp2.getSides() + mp3.getSides() + mp4.getSides( )。

public class TestParts {

public static void main(String[] args) {

    MyPolygon mp1 = new MyPolygon();
    MyPolygon mp2 = new MyPolygon(4);
    MyPolygon mp3 = new MyPolygon(5);
    MyPolygon mp4 = new MyPolygon(6);
    System.out.println("Polygon 1 has " + mp1.getSides()
            + " sides, angles total " + mp1.getSumOfAngles());
    System.out.println("Polygon 2 has " + mp2.getSides()
            + " sides, angles total " + mp2.getSumOfAngles());
    System.out.println("Polygon 3 has " + mp3.getSides()
            + " sides, angles total " + mp3.getSumOfAngles());
    System.out.println("Polygon 4 has " + mp4.getSides()
            + " sides, angles total " + mp4.getSumOfAngles());
    System.out.println("There are " + MyPolygon.getTotalSides()
            + " total sides");

}
}

class MyPolygon {
double getSides;

MyPolygon() {
    getSides = 3;
}

static double getTotalSides() {

    return 0;
}

double getSides() {
    // TODO Auto-generated method stub
    return getSides;
}

MyPolygon(double newGetSides) {
    getSides = newGetSides;
}

double getSumOfAngles() {
    return ((getSides - 2) * 180);
}

void setGetSides(double newGetSides) {
    getSides = newGetSides;
}
4

4 回答 4

1

如果你真的需要一个方法,你可以为此目的使用 varargs ( variable arity arguments ):

static double getTotalSides(MyPolygon... polygons) {
  double x = 0;

  for (MyPolygon p: polygons)
  {
   x+= p.getSides();
  }
   return x;
}

然后像这样调用:

System.out.println("There are " + MyPolygon.getTotalSides(mp1,mp2,mp3,mp4)
            + " total sides");

或者做一个数组

MyPolygon myPolygons = new MyPolygon [4];
myPolygons [0] = mp1;
myPolygons [1] = mp2;
myPolygons [2] = mp3;
myPolygons [3] = mp4;
System.out.println("There are " + MyPolygon.getTotalSides(myPolygons)
            + " total sides");

但是,更好的解决方案是从一开始就将多边形存储在数组/列表中,然后将整个数组/列表传递给方法,执行循环并返回结果。但请注意,列表和数组是不同的,因此您需要相应地修改方法签名。

于 2013-02-17T22:43:54.557 回答
0

你需要看看scope你的对象,也许你可以使用某种Collection

public class TestParts {

    public static void main(String[] args) {

        final MyPolygon mp1 = MyPolygon.newInstance(3);
        final MyPolygon mp2 = MyPolygon.newInstance(4);
        final MyPolygon mp3 = MyPolygon.newInstance(5);
        final MyPolygon mp4 = MyPolygon.newInstance(6);
        System.out.println("Polygon 1 has " + mp1.getSides()
                + " sides, angles total " + mp1.getSumOfAngles());
        System.out.println("Polygon 2 has " + mp2.getSides()
                + " sides, angles total " + mp2.getSumOfAngles());
        System.out.println("Polygon 3 has " + mp3.getSides()
                + " sides, angles total " + mp3.getSumOfAngles());
        System.out.println("Polygon 4 has " + mp4.getSides()
                + " sides, angles total " + mp4.getSumOfAngles());
        System.out.println("There are " + MyPolygon.getTotalSides()
                + " total sides");

    }
}

class MyPolygon {

    private static final Collection<MyPolygon> POLYGONS = new LinkedList<MyPolygon>();
    private final double sides;

    private MyPolygon(final double sides) {
        this.sides = sides;
    }

    public static MyPolygon newInstance(final double sides) {
        final MyPolygon polygon = new MyPolygon(sides);
        POLYGONS.add(polygon);
        return polygon;
    }

    public static double getTotalSides() {
        double sides = 0d;
        for (final MyPolygon polygon : POLYGONS) {
            sides += polygon.getSides();
        }
        return sides;
    }

    public double getSides() {
        return sides;
    }

    public double getSumOfAngles() {
        return ((sides - 2) * 180);
    }
}

我已经整理了您的代码,使其符合命名约定(无getSides变量)。

于 2013-02-17T22:45:55.320 回答
0

你为什么不把你写的表达式写进去:

System.out.println("There are " + (mp1.getSides() + mp2.getSides() + mp3.getSides() + mp4.getSides()) + " total sides");

由于您尝试将 getTotalSides 编写为类的一部分,因此没有一种简单的方法可以检查所有其他实例化对象并调用它们的方法 getSides 来将所有边相加。您可以定义一个函数来获取所有多边形对象并将边相加,但它不能在类定义下。

于 2013-02-17T22:43:43.200 回答
0

如果您想要有关已创建的所有边数的全局信息,请尝试使用静态变量:

class MyPolygon {

    private static int totalSides;
    private int sides;

    MyPolygon() {
        this(3); // constructor chaining: really useful
    }

    MyPolygon(int numSides) {
        this.sides = numSides;
        MyPolygon.totalSides += numSides;
    }

    // ... the rest, incl. getter for sides and static getter for totalSides

}

但是,当您不再需要一个多边形时,您必须以某种方式再次显式减少数字......

于 2013-02-17T22:57:48.047 回答