ArrayList<SuperClass>中是否有SubClass对象?保持子类独有的方法? [英] Does a SubClass object in an ArrayList&lt;SuperClass&gt; keep methods unique to SubClass?

查看:29
本文介绍了ArrayList<SuperClass>中是否有SubClass对象?保持子类独有的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是标题所说的,但有一些详细说明.我有一个带有几个子类的超类.我需要一个 ArrayList 来保存两种类型的子类,因此是 SuperClass 类型的 ArrayList.我尝试使用 ArrayList.get(0).getQuantity(); 访问 Subclass1 的 getQuantity() 方法(假设索引 0 是 SubClass1 类型).我收到错误消息:类型 SuperClass 的 getQuantity 未定义.

Basically what the title says, but some elaboration. I have a SuperClass with a couple of SubClasses. I needed an ArrayList to hold both types of Subclasses so hence the ArrayList of type SuperClass. I tried to access Subclass1's getQuantity() method using ArrayList.get(0).getQuantity(); (assuming that index 0 is of type SubClass1). I get the error: getQuantity is undefined for the type SuperClass.

SubClass 对象在放入 SuperClass ArrayList 时是否不保留其属性?如果他们确实保留了自己的财产,我该如何访问它们?

Do the SubClass objects not keep their properties when put into a SuperClass ArrayList? And if they do keep their properties, how do I access them?

推荐答案

对象本身仍然是一个子类,但是当你把它们从集合中取出时,它只知道超类 所以它不能告诉你哪个是哪个,它必须选择公分母.

The objects themselves are still a subclass, but when you get them out of the collection it only knows about the superclass so it can't tell you which is which, it has to pick the common denominator.

如果你确切地知道一个特定的索引包含一个 Subclass 类型的对象,你可以直接转换它:

If you know exactly that a specific index holds an object of type Subclass you can just cast it:

Subclass myObject = (Subclass) list.get(0);
System.out.println(myObject.getQuantity());

它应该可以工作.

更安全的方法是测试对象是否真的是您认为的那样:

And a safer way requires testing if the object is really what you think it is:

SuperClass myObject = list.get(0);
if ( myObject instanceof Subclass) {
  Subclass mySubObject = (Subclass) myObject;
  System.out.println(mySubObject.getQuantity());
}

如果对象不是 Subclass 类型,第一个示例会引发异常,第二个示例不会,因为它之前进行了测试以确保.

The first example raises an exception if the object is not of type Subclass, the second one wouldn't since it tests before to make sure.

这里需要理解的是,SuperClass myObject = list.get(0) 不是对象本身,而是访问内存中对象的引用.把它想象成一个允许你控制你的对象的遥控器,在这种情况下,它不是一个功能齐全的遥控器,因为它没有向你展示你的对象可以做什么,所以你可以切换到更好的一个(如 Subclass myObject = (Subclass) list.get(0)) 能够访问所有功能.

What you need to understand here is that SuperClass myObject = list.get(0) is not the object itself, but a reference to access the object in memory. Think about it as a remote that allows you to control your object, in this case, it's not a fully featured remote, since it doesn't show you all your object can do, so you can switch to a better one (as in Subclass myObject = (Subclass) list.get(0)) to be able to access all features.

我肯定会推荐 Head First Java 书籍,因为它很好地涵盖了这些内容详细信息(我从那里偷了这个远程示例).

I'd surely recommend the Head First Java book as it covers this stuff in great detail (and I stole this remote example from there).

这篇关于ArrayList<SuperClass>中是否有SubClass对象?保持子类独有的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆