使用接口 [Java] 中未定义的方法
我有一个作业,我有一个包含接口类的库。 [InfoItem]
我实现了这个类[Item]。
现在我需要编写一个方法 watchProgram(InfoItem item) [其他类,导入 InfoItem],它(如图所示)需要一个 信息项。
传递的参数项有一个变量“Recorded”[布尔值],我想使用我在 InfoItem 的实现中定义的方法 changeRecorded() 对其进行编辑。
我无法编辑界面,并且收到一条错误消息,指出找不到该方法 [找不到符号]..
有任何提示、建议、解决方案吗? 谢谢!!
-塞缪尔-
I have an assignment and i got a library including an interface class. [InfoItem]
I implement this class [Item].
Now i am required to write a method watchProgram(InfoItem item) [other class, importing InfoItem], which (as shown) requires an
InfoItem.
The passed parameter item has a variable 'Recorded' [boolean] which i want to edit using a method changeRecorded() that i defined in the implementation of InfoItem.
I cannot edit the interface and i get an error message that the method is not found [cannot find symbol]..
Any hints, suggestions, solutions?
Thanks!!
-Samuel-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在方法
watchProgram
中,Java 只知道参数是InfoItem
。该参数可能是也可能不是Item
,因此它可能有也可能没有该方法changeRecorded
。由于Java不能保证该对象具有该方法,因此它无法在编译类时插入调用该方法的代码。对于 Java 编译器而言,参数item
中不存在方法changeRecorded
。通常,当您遇到这样的情况时,这表明您实际上不应该首先调用该
changeRecorded
方法。仔细考虑为什么您认为需要它,以及如何更改自己的代码以在不使用它的情况下工作。例如,如果有人要使用没有changeRecorded
方法的InfoItem
的其他实现来调用您的watchProgram
方法,那么应该<代码>watchProgram做什么?如果经过仔细考虑,您认为当传入的参数是
Item< 的实例时,您确实有必要调用
changeRecorded
方法/code>,你可以使用强制转换来做到这一点:但正如我所说,应该谨慎使用这种东西。面向对象编程(特别是多态性)的全部意义在于让您不必这样做。
In the method
watchProgram
, all Java knows is that the argument is anInfoItem
. That argument may or may not be anItem
, and thus it may or may not have that methodchangeRecorded
. Since Java can't guarantee that the object has that method, it can't insert the code to call the method when the class is compiled. As far as the Java compiler is concerned, the methodchangeRecorded
doesn't exist in the argumentitem
.Usually when you run into a situation like this, it's a sign that you shouldn't really be calling that
changeRecorded
method in the first place. Think very carefully about why you think you need it and how you could change your own code to work without using it. For instance, if someone were to call yourwatchProgram
method with some other implementation ofInfoItem
that doesn't have achangeRecorded
method, what shouldwatchProgram
do?If, after some careful thought, you decide that it really is necessary for you to call the
changeRecorded
method when the passed-in argument is an instance ofItem
, you can use a cast to do so:But as I said, this sort of thing should be used sparingly. The whole point of object-oriented programming (specifically, polymorphism) is to make it so you don't have to do this.
您可以尝试下一个技巧,这可能是您的一个选择:
在您的 watchProgram() 方法中使用此代码。
这里我们检查参数 item 是否是 Item 类的类型,如果是则转换为该类型并调用 Item.changeRecorded() 方法。
You may try next trick, that could be an option for you:
Use this code inside of yours watchProgram() method.
Here we check whether parameter item is of type of Item class and if it's then convert to this type and call Item.changeRecorded() method.
但请注意,如果可以避免的话,这并不是一个好的做法。我会看看是否有办法仅使用接口中定义的方法来完成您想要的操作,可能是通过使更改成为其他方法的副作用。
如果您正在做的是记录已对 InfoItem 执行某些操作的事实,您也可以考虑将该事实记录在对象以外的其他位置。
However be aware that this is not good practice if it can be avoided. I would look to see if there is way to do what you want using only the methods defined in the interface, possibly by making the change a side effect of some other method.
If what you are doing is recording the fact that something has been done to InfoItem you might also consider recording the fact elsewhere than in the object.