英文:
Calling a method from a class from an element within a list of Objects
问题
背景:
我有两个类 Book
和 Video
,以及一个 Driver
类,用于保存 Book
和 Video
对象的列表,以创建一个虚拟图书馆。每个类都有一个名为 getTitle()
的方法,该方法从类构造函数的第一个参数返回给定的标题作为 String
。此外,Driver
类还有一个名为 getList()
的方法,该方法将列表(在这个上下文中是 lib
)以 List<Object>
的形式返回。
相关代码片段:
List<Object> lib = new ArrayList<Object>();
// 每个构造函数的参数都是一个字符串,代表标题,在这个示例中
Book b = new Book("Book");
Video v = new Video("Video");
lib.add(b);
lib.add(v);
所以,假设我运行以下代码:
for (int x = 0; x < lib.size(); x++) {
System.out.println(getList().get(x).getClass());
}
程序将返回
class Book
class Video
然而,如果我调用 Book
或 Video
类中的方法(方法名相同):
for (int x = 0; x < lib.size(); x++) {
System.out.println(getList().get(x).getTitle());
}
我会得到以下错误:
java: cannot find symbol
symbol: method getTitle()
location: class java.lang.Object
所以我的问题是,##
如果 getTitle()
方法不在 Object
类中,我如何从对象列表中的元素中调用 getTitle()
?如果我在 Book
或 Video
类中重写 toString()
,它会运行 Book
或 Video
中的 toString()
,就像应该的那样 - 但似乎它运行的方式在是否元素为 Object
或者我自己的类的实例之间来回变换。
我考虑过但不知道如何实现的解决方案:
将元素强制转换回 Book
或 Video
。 挑战: 如何变量地转换为多种类型,以及如何在一行中执行此操作。
使用另一个类似数组/列表的容器,可以保留 Book
和 Video
的特定数据类型。 挑战: 不熟悉,可能过于复杂化,而且我想能够使用列表。
最后的话
如果你想知道的话 - 是的,这是一个课堂作业。这就是为什么我需要一个列表/数组,而不是两个不同类型的列表/数组,这样可以让我的生活变得更简单。我没有上传所有的代码,是为了使问题尽可能简单,考虑到我有三个独特的类,而且每个类都不仅仅有一个 Title
和一个方法。如果相关的话,我可以上传实际的代码。
感谢您抽出时间与我共同思考并解决这个问题。
英文:
Background:
I have two classes Book
and Video
and a Driver
class made to hold a list of Book
and Video
objects so to have a virtual library. Each class has a method getTitle()
which returns the given title from first parameter of the class constructor as a String
. Additionally, the Driver
class has a method getList()
which returns the list (lib
in this context) as a List<Object>
Relevant Snippets:
List<Object> lib = new ArrayList<Object>();
// Params for each constructor is a single string for the title for this example
Book b = new Book("Book");
Video v = new Video("Video");
lib.add(b);
lib.add(v);
So, say I run something like:
for (int x = 0; x < lib.size(); x++) {
System.out.println(getList().get(x).getClass());
}
The program will return
class Book
class Video
However, if I call a method from the Book
or Video
class (same method name):
for (int x = 0; x < lib.size(); x++) {
System.out.println(getList().get(x).getTitle());
}
I get the following error:
java: cannot find symbol
symbol: method getTitle()
location: class java.lang.Object
So my question ultimately is,
How can I call getTitle()
from an element within a list of Objects if the getTitle()
method is not in the Object class? If I override toString()
within the Book
or Video
class, it runs the toString()
from Book
or Video
, as it should-- but it seems like the way it's running is flip-flopping between whether the elements of my list is an Object
or an instance of one my classes.
Solutions I have thought about but don't know how to implement:
Casting elements back to Book
or Video
. Challenge: How to variably cast to multiple types & how to do it inline.
Using another array/list-like container which would allow for specific data types to keep Book
and Video
. Challenge: Unfamiliarity, possible over-complication, and I'd like to be able to use lists.
Final Remarks
If you're wondering-- Yes, this is an assignment for class. That's why I need ONE list/array instead of two for each type, which would make my life much easier. The reason I didn't upload all of my code is for trying to make the question as simple as possible considering I have three unique classes I made, and there are much more than a Title
and one method for each class. If it's pertinent, I could upload the actual code.
Thank you for taking the time out of your day to think with me and work this out.
答案1
得分: 0
你不希望在这里使用对象列表;你的库旨在容纳类似库的内容。文件并不打算存储在这里。锁、输入流、整数、字符串、汽车、房屋或星系也不是。定义可以存储在库中的概念。将其定义为一个接口:
public interface LibraryWork {
String getTitle();
}
这表示:存在一个名为“库作品”的概念,所有库作品共同的地方在于它们都有标题。
然后:
class Book implements LibraryWork {
...
}
现在,你可以让你的库不再是一个Object列表,这相当无用且过于宽泛,而是一个List<LibraryWork>
。
现在你可以根据需要随意调用它的getTitle方法。
通常,避免使用.getClass
、instanceof
、类转换等操作 - 如果必须,可以将库作品种类制作成枚举,并且让你的Book类在必要时返回“BOOK”。
注意:我对“LibraryWork”这个名称不太满意,也许你可以考虑一个更好的名字。
英文:
You don't want a list of objects here; your library is intended to hold library-like things. A File isn't intended to be stored in here. Nor is a lock, an inputstream, an integer, a string, a car, a house, or a galaxy.
Define the concept of what can be stored in a library. Make this an interface:
public interface LibraryWork {
String getTitle();
}
This says: There is such a thing as the notion of 'a library work', and all library works have in common that they have titles.
Then:
class Book implements LibraryWork {
...
}
Now, you can make your library have a list not of Object, which is quite useless and overly broad, but instad, a List<LibraryWork>
.
And now you can call getTitle on it as much as you prefer.
Generally, avoid .getClass, instanceof, class casts, etc - if you must, make an enum of library work kinds and have your Book class return 'BOOK' if you must.
NB: I'm not too fond of the name LibraryWork, perhaps you can think of something better.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论