使用接口 [Java] 中未定义的方法

发布于 2024-08-30 20:15:27 字数 319 浏览 2 评论 0原文

我有一个作业,我有一个包含接口类的库。 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆夏时光 2024-09-06 20:15:27

在方法 watchProgram 中,Java 只知道参数是 InfoItem。该参数可能是也可能不是 Item,因此它可能有也可能没有该方法 changeRecorded。由于Java不能保证该对象具有该方法,因此它无法在编译类时插入调用该方法的代码。对于 Java 编译器而言,参数 item 中不存在方法 changeRecorded

通常,当您遇到这样的情况时,这表明您实际上不应该首先调用该 changeRecorded 方法。仔细考虑为什么您认为需要它,以及如何更改自己的代码以在不使用它的情况下工作。例如,如果有人要使用没有 changeRecorded 方法的 InfoItem 的其他实现来调用您的 watchProgram 方法,那么应该<代码>watchProgram做什么?

如果经过仔细考虑,您认为当传入的参数是 Item< 的实例时,您确实有必要调用 changeRecorded 方法/code>,你可以使用强制转换来做到这一点:

watchProgram(InfoItem item) {
    // do stuff
    if (item instanceof Item) { // thanks to ruslan for if statement
        Item castedItem = (Item)item;
        castedItem.changeRecorded();
    }
    // do stuff
}

但正如我所说,应该谨慎使用这种东西。面向对象编程(特别是多态性)的全部意义在于让您不必这样做。

In the method watchProgram, all Java knows is that the argument is an InfoItem. That argument may or may not be an Item, and thus it may or may not have that method changeRecorded. 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 method changeRecorded doesn't exist in the argument item.

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 your watchProgram method with some other implementation of InfoItem that doesn't have a changeRecorded method, what should watchProgram 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 of Item, you can use a cast to do so:

watchProgram(InfoItem item) {
    // do stuff
    if (item instanceof Item) { // thanks to ruslan for if statement
        Item castedItem = (Item)item;
        castedItem.changeRecorded();
    }
    // do stuff
}

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.

彼岸花似海 2024-09-06 20:15:27

您可以尝试下一个技巧,这可能是您的一个选择:

if (item instanceof Item) {
    Item myItem = (Item) item;
    myItem.changeRecorded();
}

在您的 watchProgram() 方法中使用此代码。

这里我们检查参数 item 是否是 Item 类的类型,如果是则转换为该类型并调用 Item.changeRecorded() 方法。

You may try next trick, that could be an option for you:

if (item instanceof Item) {
    Item myItem = (Item) item;
    myItem.changeRecorded();
}

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.

聊慰 2024-09-06 20:15:27
if (item instanceof Item) {
  ((Item)item).changeRecorded();
}

但请注意,如果可以避免的话,这并不是一个好的做法。我会看看是否有办法仅使用接口中定义的方法来完成您想要的操作,可能是通过使更改成为其他方法的副作用。

如果您正在做的是记录已对 InfoItem 执行某些操作的事实,您也可以考虑将该事实记录在对象以外的其他位置。

if (item instanceof Item) {
  ((Item)item).changeRecorded();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文