-2

我正在尝试包装TestClass1[]TestClass为此我创建了一个TestClass. 另一个类FunctionClass中有一个方法将返回TestClass. 我在Testclass一个是定义了一些变量,String另一个是一个类TestClass2。我将包裹TestClass2Testclass.

Testclass2也有 2 个变量,一个是String,一个是int。我想知道如何构造一个数组,Testclass其中应该包含相应的变量。

我有以下代码。

public class Testclass {

private String attrName;
    private TestClass1 tc;
    private TestClass2 tc2;

Testclass(Testclass1[] tc1){

    for(int i=0; i<tc1.length; i++){
    tc = tc1[i];
    attrName = this.tc.name; 
    tc2 = new TestClass2 (this.tc.elements); //this.tc.elements will return an Array of SomeClass which is not implemented by me.
    }

}

/**
 * Returns the Name.
 */
public String getName()
{
    return attrName;
}

/**
 * Returns the Testclass2 Array
 */
public TestClass2[] getTestClass2(){

//What to do ??
}



 /**
 * Testclass2 which is inner class of TesTClass.
 */
 private class Testclass2 {

    private int value;
    private string attribute;
    private SomeClass some;

    TestClass2 (SomeClass[] someClass){

        for(int i=0; i<someClass.length; i++){
        some= someClass[i]; 
        this.value= some.value;
                    this.attributes = some.attribute;

        }
    }

            /**
             * Returns the Value.
             */
    public int getValue(){
        return value;
    }

            /**
             * Returns the Attribute.
             */
            public string getAttribute(){
        return attribute;
    }

类中的方法Function如下:

public Testclass[] getTestClass(){

 //What to do?

}
4

2 回答 2

0

我发现你的构造函数很奇怪。你编码甚至编译?您想将一个数组分配给 tc2,但您没有选择它作为数组类型。然后使用它:

private TestClass2[] tc2;

然后在 getTestClass2 中,执行以下操作:

return tc2;
于 2012-11-19T10:30:24.057 回答
0

使类的属性TestClass成为数组。如有必要,创建getter和 setter。创建接受值或初始化数组的构造函数初始化程序。为 . 创建默认构造函数。TestClass1TestClass2TestClass2

TestClass(TestClass1[] tc1, String attrName){

    tc = tc1;
    this.attrName = attrName; 
    if (tc != null) {
      tc2 = new TestClass2[tc.length];
      for(int i = 0; i< tc.length; i++) {
        TestClass2 t2c = new TestClass2(tc[i].getElements()); //this.tc.elements will   return an Array of SomeClass which is not implemented by me.
        tc2[i] = t2c;
      }
   }
}

/**
 * Returns the Testclass2 Array
 */
public TestClass2[] getTestClass2(){

  //What to do ?? 
  return tc2;
}

class TestClass2 {
   /**
    * Default constructor
    */ 
   TestClass2(){}
于 2012-11-19T10:44:56.910 回答