1

私はJavaが初めてで、単純なサンプルプログラムの作業を始めたばかりです。

クラス B のコンストラクター内にクラス A のインスタンスを作成するにはどうすればよいですか。たとえば、クラス B のコンストラクターで、クラス A のオブジェクトの配列を作成したいとします。疑似コードは次のようになります

class B {

public static A myarray;
B (int number){
  myarray = new A [number];
}

編集:

public class TestClassA {

    public static int []  ArrayA = new int [6];
    TestClassA () {
        for (int i=0; i < 6; i++){
            ArrayA[i]=i;
            System.out.print("TestClassA ");
        }
    }
}

public class TestClassB {

    public TestClassA [] A;
    TestClassB (int num) {
        A = new TestClassA[num];
    }
}

public class Exec {

    public static void main (String[] args) {

        TestClassB B;
        B = new TestClassB(2);

    }
}

これを実行すると、「TestClassA」というメッセージは表示されません。TestClassA array の 2 つのインスタンスが作成されると予想されるため、TestClassA が 12 回表示されるはずです。どこが間違っているのかわかりません。

4

5 に答える 5

4

いくつかのポインター

  • オブジェクトの配列を class のすべてのインスタンスとstatic共有する場合を除き、 を使用しないでください。AB
  • ただし[]、参照を宣言するときに使用して、それがArray.
  • メンバー フィールドも作成privateします。次にpublicprotectedgetter/setter メソッドを介してそれらへのアクセスを制御します。

コードは次のようになります

public class B {

    private A[] arrayOfAobjects;

    B (int number) {
         arrayOfAobjects = new A[number];
    }

    public A[] getArrayOfAobjects() {
        return arrayOfAobjects;
    }
}

EDIT:(以下の@MikeStrobelのコメントを詳しく説明するため)
配列を作成すると、のタイプに従ってデフォルト値で初期化されますArray。たとえば、すべての配列要素はオブジェクト配列のすべてのタイプに対して0for anint []0.0for adouble []nullObject []

new int[100]; // creates an Array with 100 zeroes
new A[number]; // creates an Array of size "number"
               // but filled with nulls (instead of A objects)

そのため、配列に正しい値を自分で入力する必要があります。あなたの場合、次のようなもの

B (int number) {
     arrayOfAobjects = new A[number];
     for (int i=0; i < number; i++) {
         arrayOfAobjects[i] = new A(); // initialize the A[] array
     }
}

編集2

public class TestClassB {

    public TestClassA [] A;

    TestClassB (int num) {
        A = new TestClassA[num];
        for (int i=0; i < num; i++) {
            A[i] = new TestClassA(); // You need to INITIALIZE your Array
        }
    }
}
于 2013-06-18T16:25:39.970 に答える
1

クラス B のコンストラクター内でクラス A のオブジェクトを作成する場合は、次のように簡単に実行できます。

class B { 
    public A object;
    B (int number){
      object= new A();
    }

class A{

    }

A クラスの配列を作成する場合は、変数を静的にしないでください。

class B { 
        public A[] myarray;
        int number = 5;
        B (int number){
          myArray = new A[number];
        }

class A{

        }

編集:オブジェクトの配列の構文 (4 つのオブジェクトの配列が必要です)。

 A[] a = new A[4]; // Create the array of size 4.
 A a1 = new A(); //Create an object
 ............    //Similarly create other three objects
 a[0] = a1; //Add the object to the array
 ............   //Similarly add other three objects
于 2013-06-18T16:24:05.917 に答える
0
class B {

public A[] myarray;
B (int number){
  myarray = new A [number];
}

プライベート モードまたはプロテクト モードでインスタンス変数を使用し、getter および setter メソッドを使用してそれにアクセスすることを練習することをお勧めします。Aこのコードは、初期化されていないオブジェクトの配列のみを作成します。それらを使用したい場合は、どこに数字があるかのように、それらを個別に初期化する必要がmyarra[i]=new A();ありiます0<=i<number

于 2013-06-18T16:24:55.380 に答える
0

これはあなたが書くべきものです。

class B{
public A[] myArray;

B(int number){
  myArray = new A[number];
   }
  }
于 2013-06-18T16:25:51.980 に答える