【问题标题】:Instantiating different class in constructor in Java在Java的构造函数中实例化不同的类
【发布时间】:2013-06-14 23:19:31
【问题描述】:

我是 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”。我希望它创建 2 个 TestClassA array 实例,因此我应该看到 TestClassA 12 次。不知道我哪里做错了。

【问题讨论】:

  • myarray 不应该是静态的,而应该是一个数组。除此之外,您的代码很好。
  • myarray 应该被声明为一个数组 :)
  • 你能粘贴相同的代码吗...
  • 您需要像初始化int[] ArrayA 一样初始化TestClassA[] 数组。我已经在我的回答中提到了这一点。检查更新。

标签: java arrays class object constructor


【解决方案1】:

几个指针

  • 不要使用static,除非您想与B 类的每个实例共享A 对象数组。
  • 但是,您需要在声明引用时使用 [] 以表明它是 Array
  • 也将您的成员字段设为private。然后通过publicprotected getter/setter 方法控制对它们的访问。

你的代码应该是这样的

public class B {

    private A[] arrayOfAobjects;

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

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

编辑:(在下面详细说明 @MikeStrobel 的 cmets)
创建数组时,它会根据Array 的类型使用默认值进行初始化。例如,对于int [],每个数组元素都设置为0,对于double []0.0,对于所有类型的Object []对象数组,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
        }
    }
}

【讨论】:

  • 值得注意的是,由于 OP 对 Java 来说是新的,分配的数组将被 null 指针填充。也许在您的示例中添加一个初始化循环?
【解决方案2】:

如果你想在 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

【讨论】:

  • 获取类数组的系统税是什么。抱歉转储问题
  • 我能够获取数组,但是当我将它实例化为数组时,它不会影响 A 类的构造函数。我在简单的实例化中工作得很好。
【解决方案3】:
class B {

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

最好练习在私有或保护模式下使用实例变量,并使用 getter 和 setter 方法来访问它。此代码只会创建一个未初始化的 A 对象数组。如果你想使用它们,你需要像myarra[i]=new A(); 那样单独初始化它们,其中i 是任意数字0&lt;=i&lt;number

【讨论】:

    【解决方案4】:

    这是你应该写的。

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

    【讨论】:

    • class 应为小写。
    【解决方案5】:
    public class B {
    
      private A[] arrayOfAs;
    
      public B (int number) {
         this.arrayOfAs = new A[number];
      }
    
      public getAs() {
         return this.arrayOfAs;
      }
    }
    

    不高于arrayofAs不是静态的,因为您不想在B 的所有实例之间共享您的数组。我也将其设为 private,因为这被认为是实例变量的良好做法。

    您可以使用上面的getAs() 访问器方法将此数组返回到不同的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多