2013-06-18 72 views
1

我剛剛開始致力於簡單示例程序,對Java很新穎。在Java中的構造函數中實例化不同的類

如何在類B的構造函數中創建類A的實例。例如,我想在B類的構造函數中創建一個類A的對象數組。該psudo代碼看起來像

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數組的2個實例,因此我應該看到TestClassA 12次。不知道我在哪裏做錯了。

+3

'myarray'不應該是靜態的,但它應該是一個數組。除此之外,你的代碼很好。 –

+0

myarray應該聲明爲一個數組:) – Kris

+0

你可以粘貼相同的代碼... – chandank

回答

3

夫婦指針

  • 不要使用static,除非你想分享A對象的數組與類B的每個實例。
  • 但是,您確實需要使用[],同時聲明引用以指示它是Array
  • 使您的成員字段private以及。然後通過publicprotected getter/setter方法控制對它們的訪問。

你的代碼看起來應該像

public class B { 

    private A[] arrayOfAobjects; 

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

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

編輯:(細說@下面MikeStrobel的評論)
當您創建一個數組時,它會使用默認值按初始化Array的類型。例如,對於int [],0.0,對於所有類型的Object []對象陣列,每個陣列元素被設置爲0,double [],null

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 
     } 
    } 
} 
+0

值得注意的是,由於OP是Java新手,所分配的數組將被填充'null'指針。也許添加一個初始化循環到你的例子? –

1

如果要創建B類的構造函數中A類的一個對象,你可以簡單地做這樣的:

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

class 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 
+0

什麼是獲得類數組的systax。對於轉儲問題抱歉 – chandank

+0

請檢查編輯部分。 –

+0

我能夠獲得數組,但是,當我將其實例化爲數組時,它不會觸及A類的構造函數。我在簡單實例化中工作正常。 – chandank

0
class B { 

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

這將是很好的做法在私人或受保護模式中使用實例變量,並使用getter和setter方法來訪問它。此代碼只會創建一個未初始化的A對象數組。如果你想利用這些,你需要seperately初始化他們像myarra[i]=new A();其中i爲任意數量的0<=i<number

0

這是你應該寫什麼。

class B{ 
public A[] myArray; 

B(int number){ 
    myArray = new A[number]; 
    } 
    } 
+1

'class'應該是小寫。 –

0
public class B { 

    private A[] arrayOfAs; 

    public B (int number) { 
    this.arrayOfAs = new A[number]; 
    } 

    public getAs() { 
    return this.arrayOfAs; 
    } 
} 

arrayofAs上面是不是不靜態,因爲你不希望共享的B所有實例中的陣列。我也做了私人因爲這被認爲是實例變量的良好做法。

您可以使用上面的getAs()訪問器方法將此數組返回到不同的類。

相關問題