java java对象数组和继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4134970/
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
java array of objects and inheritance
提问by Luka
I have 3 classes, MainClass with main method, one abstract class named AbstractClass and Subclass which is to be extended with AbstractClass.
我有 3 个类,带有 main 方法的 MainClass,一个名为 AbstractClass 的抽象类和一个要用 AbstractClass 扩展的子类。
The array of objects is created in main method from type AbstractClass containing 1 member. Then I initialize that 1 element of array as type Subclass( is this ok?). The problem is I can't get to the getDataToExport() method of created object ( array[0] ) . I suspect the problem occurs because the array is the AbstractClass type... And the question: is this even possible to accomplish? What I'm trying to do is use an array of type AbstractClass and fill it with objects made from different subclasses( in this code is just one->Subclass ) extended with AbstractClass but i can't get to the methods of that subclasses.
对象数组是在 main 方法中从包含 1 个成员的 AbstractClass 类型创建的。然后我将数组的 1 个元素初始化为 Subclass 类型(这可以吗?)。问题是我无法访问已创建对象 ( array[0] ) 的 getDataToExport() 方法。我怀疑问题的发生是因为数组是 AbstractClass 类型......问题是:这甚至可能完成吗?我想要做的是使用 AbstractClass 类型的数组,并用由不同子类(在此代码中只是 one->Subclass )扩展的 AbstractClass 组成的对象填充它,但我无法访问该子类的方法。
main class with main method
带有主要方法的主类
public class MainClass {
public static void main() {
AbstractClass array[]=new AbstractClass[1];
array[0]= new Subclass(); // is this even allowed?
System.out.println(array[0].getDataToExport()); // Problem!
}
}
abstract class
抽象类
public abstract class AbstractClass {
}
Subclass which extends AbstractClass
扩展 AbstractClass 的子类
public class Subclass extends AbstractClass {
private int dataToExport;
public Subclass(){
this.dataToExport=2;
}
public int getDataToExport() {
return dataToExport;
}
}
回答by T.J. Crowder
AbstractClass array[]=new AbstractClass[1]; array[0]= new Subclass(); // is this even allowed?
AbstractClass array[]=new AbstractClass[1]; array[0]= new Subclass(); // is this even allowed?
Yes, that's fine, but it means that when you later go to use it, you only have access to what's defined in AbstractClass
(barring using a cast, but you want to avoid using casts wherever you can, and there's no need for one here).
是的,这很好,但这意味着当您以后使用它时,您只能访问中定义的内容AbstractClass
(除非使用强制转换,但您希望尽可能避免使用强制转换,这里不需要一个) .
The only real reason for making the array entries of type AbstractClass
would be if you only want to interact with the members defined in that class. So for instance, in this case, you'd probably want to have the getDataToExport
defined as an abstract method in the abstract class:
制作类型数组条目的唯一真正原因AbstractClass
是,如果您只想与该类中定义的成员进行交互。因此,例如,在这种情况下,您可能希望将getDataToExport
定义为抽象类中的抽象方法:
public abstract class AbstractClass {
public abstract int getDataToExport();
}
You might also consider looking at having an interfacerather than an abstract class. Since a class can only derive from one base class, but can implement as many interfaces as it likes, unless there's a large body of common implementation that you'd be putting in the abstract base class, you're better off with an interface — because it doesn't put unnecessary constraints on the implementations of that interface. In fact, you're almost always better off with an interface; you can always alsohave an abstract base if you want.
您也可以考虑使用接口而不是抽象类。由于一个类只能从一个基类派生,但可以实现任意数量的接口,除非您将大量通用实现放入抽象基类中,否则最好使用接口——因为它不会对该接口的实现施加不必要的限制。事实上,使用界面几乎总是更好。如果你愿意,你也可以有一个抽象的基础。
So for instance:
所以例如:
public class MainClass {
public static void main() {
NiftyThingy array[]=new NiftyThingy[1];
array[0]= new NiftyThingyImplementation();
System.out.println(array[0].getDataToExport());
}
}
where
在哪里
public interface NiftyThingy {
public int getDataToExport();
}
and
和
public class NiftyThingyImplementation implements NiftyThingy {
public int getDataToExport() {
return /* ... the data ... */;
}
}
回答by Buhb
You must declare getDataToExport() as an abstract method in AbstractClass.
您必须在 AbstractClass 中将 getDataToExport() 声明为抽象方法。
回答by Alfredo Osorio
You won't be able to get it because you are declaring an array of AbstractClass in order to see them you have two choices
您将无法获得它,因为您正在声明一个 AbstractClass 数组以便查看它们,您有两种选择
Declare the common methods that you will use on the abstract class and declare them abstract. Override those methods in the subclasses. For example:
public abstract class AbstractClass { public abstract int getDataToExport(); }
声明您将在抽象类上使用的常用方法并将它们声明为抽象的。覆盖子类中的这些方法。例如:
公共抽象类 AbstractClass { 公共抽象 int getDataToExport(); }
And every subclass will have to override that method or should be declared abstract too.
并且每个子类都必须覆盖该方法或者也应该声明为抽象的。
Create an array of Sublclass:
Subclass array[] = new Sublcass[1]; array[0] = new Subclass(); System.out.println(array[0].getDataToExport());
创建一个子类数组:
子类数组[] = 新子类[1]; 数组[0] = 新子类(); System.out.println(array[0].getDataToExport());
回答by DJClayworth
Yes, all this is perfectly fine. The only step you have missed is that you need to castthe instance of the object to the type that has the method you want. The usual way to do this is:
是的,这一切都很好。你已经错过了最后一个步骤是,你需要投对象的实例有需要的方法类型。通常的做法是:
AbstractClass ac = array[0];
if (ac instanceof Subclass) {
System.out.println(((Subclass)ac).getDataToExport()); // No problem!
}
However you might want to think about doing this another way. For example, implement a default version of getDataToExport in the abstract class that returns null. That way you don't have to do the cast or the instanceof test.
但是,您可能想考虑以另一种方式执行此操作。例如,在返回 null 的抽象类中实现 getDataToExport 的默认版本。这样您就不必进行强制转换或 instanceof 测试。
回答by Buhake Sindi
AbstractClass array[]=new AbstractClass[1];
array[0]= new Subclass(); // is this even allowed?
Indeed is valid as SubClass
is a derived class of AbstractClass
. The reason why
Indeed 和SubClass
的派生类一样有效AbstractClass
。之所以
System.out.println(array[0].getDataToExport());
fails is because the compiler cannot find AbstractClass.getDataToExport()
method. It's only SubClass
that has the method and not its parent.
失败是因为编译器找不到AbstractClass.getDataToExport()
方法。只有SubClass
那个有方法而不是它的父级。
There are couple of ways to solve this:
有几种方法可以解决这个问题:
Add an abstract method getDataToExport
to the AbstractClass
like so:
一个抽象的方法添加getDataToExport
到AbstractClass
像这样:
public abstract class AbstractClass {
public abstract int getDataToExport();
}
OR, Typecastyour variable to its derived (concrete) class that has the getDataToExport()
method, like so:
OR,强制转换的变量及其衍生(混凝土)类具有getDataToExport()
方法,像这样:
if (array[0] instanceof SubClass) {
System.out.println(((SubClass)array[0]).getDataToExport());
}
the instanceof
operator simply states that the attribute is of a class X (where X is SubClass
).
的instanceof
操作者只需指出该属性是一个类X(其中X是的SubClass
)。