java 对象数组和继承
我有 3 个类,一个带有 main 方法的 MainClass,一个名为 AbstractClass 的抽象类和一个用 AbstractClass 扩展的子类。
对象数组是在 main 方法中从包含 1 个成员的 AbstractClass 类型创建的。 然后我将数组的 1 个元素初始化为 Subclass 类型(这样可以吗?)。 问题是我无法访问创建的对象( array[0] )的 getDataToExport() 方法。 我怀疑出现问题是因为数组是 AbstractClass 类型... 问题是:这有可能实现吗? 我想做的是使用 AbstractClass 类型的数组,并用由 AbstractClass 扩展的不同子类(在这段代码中只是 one->Subclass )创建的对象填充它,但我无法访问该子类的方法。
具有主方法的主类
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!
}
}
抽象类
public abstract class AbstractClass {
}
扩展 AbstractClass 的子类
public class Subclass extends AbstractClass {
private int dataToExport;
public Subclass(){
this.dataToExport=2;
}
public int getDataToExport() {
return dataToExport;
}
}
I have 3 classes, MainClass with main method, one abstract class named AbstractClass and Subclass which is to be extended with 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 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
public class Subclass extends AbstractClass {
private int dataToExport;
public Subclass(){
this.dataToExport=2;
}
public int getDataToExport() {
return dataToExport;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这很好,但这意味着当您稍后使用它时,您只能访问
AbstractClass
中定义的内容(禁止使用强制转换,但您希望尽可能避免使用强制转换,这里不需要一个)。制作
AbstractClass
类型的数组条目的唯一真正原因是,如果您只想与该类中定义的成员进行交互。例如,在这种情况下,您可能希望将getDataToExport
定义为抽象类中的抽象方法:您还可以考虑拥有一个接口而不是抽象类。由于一个类只能从一个基类派生,但可以实现任意多个接口,除非您要在抽象基类中放入大量通用实现,否则最好使用接口——因为它不会对该接口的实现施加不必要的限制。事实上,使用界面几乎总是更好。如果您愿意,您也可以随时拥有一个抽象基础。
例如:
哪里
和
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).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 thegetDataToExport
defined as an abstract method in the abstract class:You might also consider looking at having an interface rather 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 also have an abstract base if you want.
So for instance:
where
and
您必须将 getDataToExport() 声明为 AbstractClass 中的抽象方法。
You must declare getDataToExport() as an abstract method in AbstractClass.
您将无法获得它,因为您要声明一个 AbstractClass 数组才能看到它们,您有两个选择
声明将在抽象类上使用的通用方法并将它们声明为抽象。重写子类中的这些方法。例如:
公共抽象类 AbstractClass {
公共抽象 int getDataToExport();
}
都必须重写该方法或者也应该声明为抽象方法。
创建子类数组:
子类数组[] = new Sublcas[1];
数组[0] = new 子类();
System.out.println(array[0].getDataToExport());
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
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();
}
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());
是的,这一切都很好。您错过的唯一一步是您需要将对象的实例强制转换为具有所需方法的类型。通常的方法是:
但是您可能想考虑以其他方式执行此操作。例如,在返回 null 的抽象类中实现 getDataToExport 的默认版本。这样您就不必进行强制转换或实例测试。
Yes, all this is perfectly fine. The only step you have missed is that you need to cast the instance of the object to the type that has the method you want. The usual way to do this is:
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.
事实上是有效的,因为
SubClass
是AbstractClass
的派生类。 失败的原因是编译器找不到
AbstractClass.getDataToExport()
方法。只有SubClass
具有该方法,而不是其父类。有几种方法可以解决这个问题:
将抽象方法
getDataToExport
添加到AbstractClass
,如下所示:OR,Typecast > 将变量传递给具有 getDataToExport() 方法的派生(具体)类,如下所示:
instanceof 运算符只是声明该属性属于类 X(其中X 是
子类
)。Indeed is valid as
SubClass
is a derived class ofAbstractClass
. The reason whyfails is because the compiler cannot find
AbstractClass.getDataToExport()
method. It's onlySubClass
that has the method and not its parent.There are couple of ways to solve this:
Add an abstract method
getDataToExport
to theAbstractClass
like so:OR, Typecast your variable to its derived (concrete) class that has the
getDataToExport()
method, like so:the
instanceof
operator simply states that the attribute is of a class X (where X isSubClass
).