从方法中获取值(java)

时间:2013-02-17 22:40:05

标签: java methods

到目前为止,我已经得到了这个代码,但我正在尝试学习如何完成此方法,以便它将编译所使用的边的总量并在最后一个打印行中打印该值。问题方法是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 个答案:

答案 0 :(得分: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");

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

答案 1 :(得分:0)

为什么不把你写的表达式放在:

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

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

答案 2 :(得分: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变量)。

答案 3 :(得分: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

}

然而,当你可能不再需要一个多边形时,你必须以某种方式明确地减少数字......