如何在实例方法中使用数组?

时间:2014-12-16 01:43:23

标签: java arrays methods instance

  public class APPoint 
  { 
    private double myX; 
    private double myY; 

    public APPoint( double x, double y ) 
    { 
      myX = x; 
      myY = y; 
    } 

    public void move( double x, double y ) 
    { 
      myX += x; 
      myY += y; 
    } 

    public APPoint shiftCopy( double x, double y ) 
    {  
      return new APPoint( myX + x, myY + y ); 
    }


**My Code**

APPoint[] compassPoints = new APPoint{ I Don't know what to put here };
 return ??;





    public double getX()  
    { 
      return myX; 
    } 

    public void setX( double x ) 
    { 
      myX = x; 
    } 

    public double getY() 
    { 
      return myY; 
    } 

    public void setY( double y ) 
    { 
      myY = y; 
    } 
  } 
public static void main( String[] args )
{
  APPoint a = new APPoint( 1.0, 3.0 );
  APPoint b = a.shiftCopy( -2.0, 2.0 );
  APPoint c = a.shiftCopy( 3.0, 1.0 ).shiftCopy( 4.0, -5.0 );


}

我的方向是添加一个名为compassPoints的实例方法,该方法返回一个由四个APPoint对象组成的数组,每个对象距离北,东,南和西方向的实例距离为1,然后compassPoints应该返回一个长度为4的数组其元素是APPoint对象,坐标为:(12.5,-7.1),(13.5,-8.1),(12.5,-9.1)和(11.5,-8.1)。

如果有人可以帮我解释一下我应该在我的代码部分中提出什么,我将非常感激。谢谢。

2 个答案:

答案 0 :(得分:0)

APPoint[] compassPoints = new APPoint[]{
    new APPoint(12.5, -7.1),
    new APPoint(13.5, -8.1),
    new APPoint(12.5, -9.1),
    new APPoint(11.5, -8.1) };

答案 1 :(得分:0)

这样的东西
APPoint[] compassPoints = {
  new APPoint(12.5, -7.1),new APPoint(13.5, -8.1),
  new APPoint(12.5, -9.1),new APPoint(11.5, -8.1)
};

您可以在JLS-10.6 Array Initializers找到其他示例。