如何添加以int数组作为参数的对象

时间:2014-02-11 01:38:54

标签: java arrays object

对于我的一个类中的赋值,我必须添加两个由int数组组成的Poly对象。我有一个名为add的方法,它应该将Poly作为参数并将其添加到当前Poly对象(Poly1.add(Poly2)),Poly表示多项式,因此它应该添加每个数组的系数。如何从Poly中获取每个数组以便我可以添加它们?

public class Poly
{
    private int[] coefficients2;

    public static void main(String[] args){

    }

    Poly(int[] coefficients){
        coefficient2 = coefficients;
    }

    public Poly add(Poly a)
    {
        Poly b = new Poly(coefficientList);
        Poly c = null;
        if (coefficientList.length-1 >= a.degree())
        {
            c = new Poly(new int[coefficientList.length-1]); 
        }
        else if (coefficientList.length-1 < a.degree())
        {
            c = new Poly(new int[a.degree()]);
        }
        //This is where I don't know what to do.
    }
}

1 个答案:

答案 0 :(得分:1)

如果某个对象在同一个类的另一个实例上运行,它可以直接访问另一个实例中的任何内容......或者您可以定义访问所需值或数据结构的getter方法。