Java 抽象类数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19310638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array of abstract class
提问by Nitin
Why can I not instantiate an abstract class but make an array of the abstract class?
为什么我不能实例化一个抽象类,而是创建一个抽象类的数组?
public abstract class Game{
...
}
Game games = new Game(); //Error
Game[] gamesArray = new Game[10]; //No Error
采纳答案by SudoRahul
Game[] gamesArray = new Game[10];
Instantiation means creation of an instance of a class. In the above scenario, you've just declared a gamesArray
of type Game
with the size 10
(just the references and nothing else). That's why its not throwing any error.
实例化意味着创建一个类的实例。在上面的场景中,您刚刚声明了具有大小gamesArray
的类型(仅引用而不是其他任何内容)。这就是为什么它不会抛出任何错误。Game
10
You'll get the error when you try to do
当你尝试做时你会得到错误
gamesArray[0] = new Game(); // because abstract class cannot be instantiated
but make an array of the abstract class?
但是制作一个抽象类的数组?
Later on, you can do something like this
稍后,你可以做这样的事情
gamesArray[0] = new NonAbstractGame(); // where NonAbstractGame extends the Games abstract class.
This is very much allowed and this is why you'll be going in for an abstract class on the first place.
这是非常允许的,这就是为什么您首先要进入抽象类的原因。
回答by Dawood ibn Kareem
Because when you make an array of some object type, you're not trying to instantiate the objects. All you're making is a number of slots to put references in.
因为当您创建某个对象类型的数组时,您并没有尝试实例化这些对象。您所做的只是一些用于放置引用的插槽。
new Game[10];
makes 10 slots for Game
references, but it doesn't make a single Game
.
new Game[10];
为Game
参考制作 10 个插槽,但它不会制作单个Game
.
回答by Silviu Burcea
Abstract classes cannot be instantiated, they can be extended. Arrays are in fact objects, you just say to your JVM : hey buddy, make some room for 10 Game objects. That's all, you don't instantiate any Game objects.
抽象类不能被实例化,它们可以被扩展。数组实际上是对象,您只需对 JVM 说:嘿伙计,为 10 个游戏对象腾出空间。就是这样,您没有实例化任何 Game 对象。
回答by user2339071
Because you don't violate the abstract class
rules.Essentially,
因为你没有违反abstract class
规则。本质上,
Game games = new Game();
is broken down to:
被分解为:
Game games; //Will Work because it's just a declaration
games=new Game(); //Will not work because it's instantiation
While creating objects is perfectly valid for abstract classes, initializing is not permitted.
虽然创建对象对于抽象类完全有效,但不允许初始化。
回答by Tej Kiran
Game games = new Game(); This is creating instance of Abstract Class Game which is not allowed.
游戏游戏 = 新游戏();这是创建抽象类游戏的实例,这是不允许的。
Creating array with following Game[] gamesArray = new Game[10];
使用以下 Game[] gamesArray = new Game[10] 创建数组;
is just as a declaring Game Object here it is not creating instance. like Game game;
就像在这里声明游戏对象一样,它不创建实例。喜欢游戏游戏;
Either you declare 10 object of Game class or crating array of game both are same, just allocation of memory will be different.
无论是声明 Game 类的 10 个对象还是游戏的 crating 数组都是相同的,只是内存分配会有所不同。
Thanks
谢谢
Tej Kiran
泰姬兰
回答by Prateek
Abstract Class as name implies can't be instantiated. When you did Game[] gamesArray = new Game[10];
it is creating an array
which can hold Game references
. Lets try to relate it to a real life scenario.
顾名思义,抽象类不能被实例化。当您这样做时,Game[] gamesArray = new Game[10];
它正在创建一个array
可以容纳Game references
. 让我们尝试将其与现实生活场景联系起来。
Say you have an abstract class Animal
. You can't instantiate it as animal is not a concrete class so Animal obj = new Animal()
would fail. But Animal[] animalArray = new Animal[10]
would create an array which can hold references to 10 animal type objects like dog, cat, horse and so on.
假设您有一个抽象类Animal
。你不能实例化它,因为动物不是一个具体的类,所以Animal obj = new Animal()
会失败。但是Animal[] animalArray = new Animal[10]
会创建一个数组,该数组可以保存对 10 个动物类型对象(如狗、猫、马等)的引用。
回答by Kumar Vivek Mitra
-An Abstract
class is one whose instance CANNOT be created.
-Abstract
类是不能创建其实例的类。
-Creating an Array which holds the Object Reference Variable of that Abstract class are just the references not the object itself.
-创建一个包含该抽象类的对象引用变量的数组只是引用而不是对象本身。
-An Abstract
class as well as an Interface
in Java is used for implementing behaviors which keeps changing. Consider the Abstract class like laying down some protocols for its concrete classes.
-一Abstract
类,以及一个Interface
在Java中用于实现其不断变化的行为。考虑Abstract class like laying down some protocols for its concrete classes.
Eg:
例如:
public abstract class Vehicle{
}
public class Car extends Vehicle{
public static void main(String[] args){
Car car1 = new Car(); (Possible)
Vehicle car2 = new Car(); (Possible)
Vehicle car3 = new Vehicle(); (NOT Possible)
}
}
回答by MSalmo
Abstract classes do not have methods entirely fleshed out. They are used as base classes that would be further extended by other classes that "inherit" them.
抽象类没有完全充实的方法。它们被用作基类,这些基类将被“继承”它们的其他类进一步扩展。
For instance, you can have an abstract class called Pet.
例如,您可以有一个名为 Pet 的抽象类。
public abstract class Pet{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
abstract String makeNoise();
}
That can be extended by inherited classes:
可以通过继承的类来扩展:
public class Cat extends Pet {
public Cat(){ ... }
...
public String makeNoise(){
return "Meow!";
}
}
...
public class Dog extends Pet {
public Cat(){ ... }
...
public String makeNoise(){
return "Bark!";
}
}
From there, you can have things in your code such as:
从那里,您可以在代码中包含以下内容:
Pet thisPet = new Dog();
...
thisPet = new Cat();
And you can create arrays of Pets which would take in both Cats and Dogs as acceptable elements.
并且您可以创建 Pets 数组,将 Cats 和 Dogs 作为可接受的元素。
Pet petArr[] = new Pet[2];
petArr[0] = new Cat();
petArr[1] = new Dog();
For more information, I'd recommend you look at the java tutorial section covering abstract classes here.
有关更多信息,我建议您在此处查看涵盖抽象类的 Java 教程部分。