We can't create objects of an abstract class, but we can create a List or an array of them. What is the difference?
我们不能创建抽象类的对象,但我们可以创建List或它们的数组。有什么不同?
8 个解决方案
#1
15
A list or array is simply a place holder for a set of pointers, and you have NOT created instances of anything yet.
列表或数组只是一组指针的占位符,您还没有创建任何实例。
When you say Create Object - you mean create an instance - which you cannot do with an abstract class.
当你说Create Object时 - 你的意思是创建一个实例 - 你不能用抽象类做。
But you can create Lists or Arrays that point to them (and are EMPTY) - then you can move the pointers to 'real' instances of derived classes/objects
但是你可以创建指向它们的列表或数组(并且是EMPTY) - 然后你可以将指针移动到派生类/对象的“真实”实例
#2
8
Summary of Answer:
答案摘要:
The List or the array itself is not an abstract class, even if the type it declares it will hold is an abstract class.
List或数组本身不是一个抽象类,即使它声明它将保存的类型是一个抽象类。
More Detailed Answer:
更详细的答案:
Even though you can't make an object of an abstract class (the word "of" is somewhat vague, but obviously you mean as a direct instantiation of that class), you can make an object that is an instance of an abstract class (because it's an instance of a concrete class that extends said abstract class).
即使你不能创建一个抽象类的对象(“of”这个词有些模糊,但显然你的意思是作为该类的直接实例化),你可以创建一个对象,它是一个抽象类的实例(因为它是扩展所述抽象类的具体类的实例。
Basically, given abstract class Abstraction
, and concrete class Concrete
that extends Abstraction
, List<Abstraction>
means you can but anything in it that is an instance of something that extends Abstraction
, e.g. an instance of Concrete
. Although arrays are a bit different in some ways from Lists, the same goes for them as well.
基本上,给定抽象类Abstraction,以及扩展Abstraction的具体类Concrete,List
Example:
Given this class:
鉴于此课程:
public abstract class Abstraction {
}
And this class:
而这堂课:
public class Concrete : Abstraction {
}
You can't do this:
你不能这样做:
Abstraction a = new Abstraction();
But you CAN do this:
但你可以这样做:
Abstraction a = new Concrete();
And then you can do this:
然后你可以这样做:
IList<Abstraction> list = new List();
list.add(a);
#3
6
It is possible to declare and initialise a list where the type of the members is an abstract class because to create this object you do not need to actually instantiate the abstract class.
可以声明并初始化一个列表,其中成员的类型是抽象类,因为要创建此对象,您不需要实际实例化抽象类。
Logically, the list is only a container for pointers.
从逻辑上讲,列表只是指针的容器。
For example, you can define a list of an abstract class, but this list can be at the beginning empty.
例如,您可以定义抽象类的列表,但此列表可以在开头为空。
You can also initialise the list with an initial size, but at the beginning it will not contain references to actual objects: the initialisation will only preallocate space for the pointers.
您也可以使用初始大小初始化列表,但在开始时它不会包含对实际对象的引用:初始化将仅预先为指针分配空间。
The objects added to the list have to be instances of a concrete class that extends the abstract class.
添加到列表中的对象必须是扩展抽象类的具体类的实例。
#4
3
You can't create an instance of an abstract class for obvious reasons but surely creating an instance of the List<T>
class is allowed regardless that T is an abstract class.
由于显而易见的原因,您无法创建抽象类的实例,但无论T是抽象类,都必须创建List
Let the abstract class be called ClassAbstract
.
让抽象类称为ClassAbstract。
- When you try to do -
new ClassAbstract()
, you try to create an object of theClassAbstract
. - When you do -
new List<ClassAbstract>()
, you create an object of theList<T>
class.
当您尝试执行新的ClassAbstract()时,您尝试创建ClassAbstract的对象。
当您执行新的List
#5
2
I believe u are simply strongly typing what the objects inside the array will be
我相信你只是强烈地键入数组中的对象
So when it comes time to put instantiated objects into the array. It has to be an object that was extended from the abstract class
因此,当将实例化对象放入数组时。它必须是从抽象类扩展的对象
#6
2
The List
is empty !
清单是空的!
That means no instance of the abstract
class.
这意味着没有抽象类的实例。
Create an empty list is allowed. You can (later) add to it a non abstract
class which extends it.
允许创建一个空列表。您可以(稍后)向其添加一个扩展它的非抽象类。
#7
2
There are no pink magic unicorns.
没有粉红色的魔法独角兽。
But you can still build a stable for pink magic unicorns, you just won't ever be able to put any in there, because there aren't any.
但你仍然可以为粉红魔法独角兽建立一个稳定的,你永远不会放任何东西,因为没有。
More realistic analogy: you cannot go to the pet store and buy "a mammal". That's far too abstract a thing.
更现实的比喻:你不能去宠物商店买“哺乳动物”。这太抽象了。
But, you can build a stable for mammals. And then you can buy horses, cows, dogs, cats, elephants, giraffes, wolves, leopards, and lions and put them in there. Maybe even pink magic unicorns, if you can find any.
但是,你可以为哺乳动物建立一个稳定的。然后你可以买马,牛,狗,猫,大象,长颈鹿,狼,豹子和狮子,并把它们放在那里。也许甚至是粉红色的魔法独角兽,如果你能找到的话。
(How to build a stable that can at the same time accommodate mice and hump whales is left as an exercise to the reader.)
(如何建造一个可以同时容纳老鼠和驼背鲸的马厩,留给读者作为练习。)
#8
0
No, you can't create an array "of them", because you can't put the abstract types into the list. A list of an abstract type holds any derived instances of the abstract type.
不,你不能创建一个“他们”的数组,因为你不能将抽象类型放入列表中。抽象类型的列表包含抽象类型的任何派生实例。
Just means any derived type of the abstract type can be in the list
只是意味着抽象类型的任何派生类型都可以在列表中
#1
15
A list or array is simply a place holder for a set of pointers, and you have NOT created instances of anything yet.
列表或数组只是一组指针的占位符,您还没有创建任何实例。
When you say Create Object - you mean create an instance - which you cannot do with an abstract class.
当你说Create Object时 - 你的意思是创建一个实例 - 你不能用抽象类做。
But you can create Lists or Arrays that point to them (and are EMPTY) - then you can move the pointers to 'real' instances of derived classes/objects
但是你可以创建指向它们的列表或数组(并且是EMPTY) - 然后你可以将指针移动到派生类/对象的“真实”实例
#2
8
Summary of Answer:
答案摘要:
The List or the array itself is not an abstract class, even if the type it declares it will hold is an abstract class.
List或数组本身不是一个抽象类,即使它声明它将保存的类型是一个抽象类。
More Detailed Answer:
更详细的答案:
Even though you can't make an object of an abstract class (the word "of" is somewhat vague, but obviously you mean as a direct instantiation of that class), you can make an object that is an instance of an abstract class (because it's an instance of a concrete class that extends said abstract class).
即使你不能创建一个抽象类的对象(“of”这个词有些模糊,但显然你的意思是作为该类的直接实例化),你可以创建一个对象,它是一个抽象类的实例(因为它是扩展所述抽象类的具体类的实例。
Basically, given abstract class Abstraction
, and concrete class Concrete
that extends Abstraction
, List<Abstraction>
means you can but anything in it that is an instance of something that extends Abstraction
, e.g. an instance of Concrete
. Although arrays are a bit different in some ways from Lists, the same goes for them as well.
基本上,给定抽象类Abstraction,以及扩展Abstraction的具体类Concrete,List
Example:
Given this class:
鉴于此课程:
public abstract class Abstraction {
}
And this class:
而这堂课:
public class Concrete : Abstraction {
}
You can't do this:
你不能这样做:
Abstraction a = new Abstraction();
But you CAN do this:
但你可以这样做:
Abstraction a = new Concrete();
And then you can do this:
然后你可以这样做:
IList<Abstraction> list = new List();
list.add(a);
#3
6
It is possible to declare and initialise a list where the type of the members is an abstract class because to create this object you do not need to actually instantiate the abstract class.
可以声明并初始化一个列表,其中成员的类型是抽象类,因为要创建此对象,您不需要实际实例化抽象类。
Logically, the list is only a container for pointers.
从逻辑上讲,列表只是指针的容器。
For example, you can define a list of an abstract class, but this list can be at the beginning empty.
例如,您可以定义抽象类的列表,但此列表可以在开头为空。
You can also initialise the list with an initial size, but at the beginning it will not contain references to actual objects: the initialisation will only preallocate space for the pointers.
您也可以使用初始大小初始化列表,但在开始时它不会包含对实际对象的引用:初始化将仅预先为指针分配空间。
The objects added to the list have to be instances of a concrete class that extends the abstract class.
添加到列表中的对象必须是扩展抽象类的具体类的实例。
#4
3
You can't create an instance of an abstract class for obvious reasons but surely creating an instance of the List<T>
class is allowed regardless that T is an abstract class.
由于显而易见的原因,您无法创建抽象类的实例,但无论T是抽象类,都必须创建List
Let the abstract class be called ClassAbstract
.
让抽象类称为ClassAbstract。
- When you try to do -
new ClassAbstract()
, you try to create an object of theClassAbstract
. - When you do -
new List<ClassAbstract>()
, you create an object of theList<T>
class.
当您尝试执行新的ClassAbstract()时,您尝试创建ClassAbstract的对象。
当您执行新的List
#5
2
I believe u are simply strongly typing what the objects inside the array will be
我相信你只是强烈地键入数组中的对象
So when it comes time to put instantiated objects into the array. It has to be an object that was extended from the abstract class
因此,当将实例化对象放入数组时。它必须是从抽象类扩展的对象
#6
2
The List
is empty !
清单是空的!
That means no instance of the abstract
class.
这意味着没有抽象类的实例。
Create an empty list is allowed. You can (later) add to it a non abstract
class which extends it.
允许创建一个空列表。您可以(稍后)向其添加一个扩展它的非抽象类。
#7
2
There are no pink magic unicorns.
没有粉红色的魔法独角兽。
But you can still build a stable for pink magic unicorns, you just won't ever be able to put any in there, because there aren't any.
但你仍然可以为粉红魔法独角兽建立一个稳定的,你永远不会放任何东西,因为没有。
More realistic analogy: you cannot go to the pet store and buy "a mammal". That's far too abstract a thing.
更现实的比喻:你不能去宠物商店买“哺乳动物”。这太抽象了。
But, you can build a stable for mammals. And then you can buy horses, cows, dogs, cats, elephants, giraffes, wolves, leopards, and lions and put them in there. Maybe even pink magic unicorns, if you can find any.
但是,你可以为哺乳动物建立一个稳定的。然后你可以买马,牛,狗,猫,大象,长颈鹿,狼,豹子和狮子,并把它们放在那里。也许甚至是粉红色的魔法独角兽,如果你能找到的话。
(How to build a stable that can at the same time accommodate mice and hump whales is left as an exercise to the reader.)
(如何建造一个可以同时容纳老鼠和驼背鲸的马厩,留给读者作为练习。)
#8
0
No, you can't create an array "of them", because you can't put the abstract types into the list. A list of an abstract type holds any derived instances of the abstract type.
不,你不能创建一个“他们”的数组,因为你不能将抽象类型放入列表中。抽象类型的列表包含抽象类型的任何派生实例。
Just means any derived type of the abstract type can be in the list
只是意味着抽象类型的任何派生类型都可以在列表中