简体   繁体   English

我如何创建抽象 class 的对象?

[英]How I created objects of an abstract class?

  1. Why GeometricObject[] abstractObjects = new GeometricObject[10];为什么GeometricObject[] abstractObjects = new GeometricObject[10]; don't cause error that I create objects of an abstract class?不要导致我创建抽象 class 的对象的错误?
  2. What's the difference between A[] objects; A[] objects; and A[] objects = new A[7];A[] objects = new A[7];
public class Test
{
    public static void main(String[] args)
    {
        int[] numbers = new int[8];
        A[] objects = new A[7];
        GeometricObject[] abstractObjects = new GeometricObject[10];
        System.out.println(numbers[3]); // prints 0
        System.out.println(objects[4]); // prints null
        System.out.println(abstractObjects[6]); // prints null
    }
}

abstract class GeometricObject {}

class A
{
    public int number;
    A()
    {
        number = 13;
    }
}
 GeometricObject[] abstractObjects = new GeometricObject[10];

you are not creating object of GeometricObject here.您不是在此处创建 GeometricObject 的 object。 You are creating an object of an array(of type GeometricObject class ).您正在创建一个数组的 object(类型为GeometricObject class )。

What's the difference between A[] objects; A[] 对象之间有什么区别; and A[] objects = new A[7];和 A[] 对象 = 新 A[7];

A[] objects = new A[2] is divided in two parts A[] objects = new A[2]分为两部分

A[] objects - its called deceleration of object. A[] objects - 它称为 object 的减速。

new A[7] - called initialization of object new A[7] - 调用 object 的初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM