C#抽象类,适用于数组初始化

时间:2010-10-27 12:34:06

标签: c# oop design-patterns abstract-class

我们知道我们无法创建abstract class

的实例

我只想知道如果我们创建抽象类数组,它肯定会有用。

E.g。

public abstract class Creator
{
    public abstract void DoSomething();
}

Creator creator = new Creator(); // this will give you compilation error!

Creator[] creator = new Creator[2]; // this will SURE work and will NOT give you compilation error.

任何人都可以告诉我为什么会发生这种情况以及它为什么要使用数组初始化?

提前致谢。

4 个答案:

答案 0 :(得分:6)

初始化期间存储在数组中的值都是null,因此实际上并不创建抽象类的任何实例。数组初始化对应于以下(正确)行:

Creator creator = null;

创建类型AbstractClass[]的数组实际上非常有用,因为您可以将引用存储到数组中的某个具体(继承)类。例如:

var objects = new object[2];
objects[0] = "Hello";
objects[1] = new System.Random();

然后你可以迭代objects数组并在所有对象上调用ToString()

答案 1 :(得分:3)

这里要记住的是,你是不是在创建Creator的任何实例,而是创建了这些类型的数组,其值是null

答案 2 :(得分:1)

Creator[] creator = new Creator[2];

只需初始化一个Array即可保存Creator类的两个实例。它实际上并没有为您实例化该类的任何实例。在您初始化它们之前,这两个元素仍然是null

答案 3 :(得分:0)

C#数组中的

与基本类型不同。 07-22 20:14:49.267 15607 15631 I Unity : AndroidJavaException: java.lang.ClassNotFoundException: com.google.android.gms.ads.AdSize 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject._CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject.CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject.FindClass (System.String name) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject..ctor (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at GoogleMobileAds.Android.Utils.Get07-22 20:14:49.267 15607 15631 I Unity : AndroidJavaException: java.lang.ClassNotFoundException: com.google.android.gms.ads.AdSize 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject._CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject.CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject.FindClass (System.String name) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at UnityEngine.AndroidJavaObject..ctor (System.String className, System.Object[] args) [0x00000] in <filename unknown>:0 07-22 20:14:49.267 15607 15631 I Unity : at GoogleMobileAds.Android.Utils.Get Creator是两种不同的类型。 只有Creator[]是抽象的。 Creator不是抽象的,这就是为什么您可以创建它的实例。