使用Java中的异常伪造抽象方法 [英] Faking abstract methods using exceptions in Java

查看:118
本文介绍了使用Java中的异常伪造抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人问过我这个问题,很坦率地说:

I've been asked this question and am frankly stumped:

除了编写抽象方法外,我们还可以尝试用异常来伪造它们,就像这样:

public int someMethod(int someparameter) {
    throws new RuntimeException("unimplemented");
}

这样做的目的是什么,任何人都可以提供示例代码以上试图伪造的抽象方法?任何帮助是极大的赞赏。谢谢。

What would be the purpose of doing this and can anyone provide an example of code for an abstract method that the above is trying to fake? Any help is greatly appreciated. Thank you.

推荐答案

我可以想到一个(可能是)有效的用例:您有一个抽象类,还有许多整个项目中的其他类扩展了它。 (或者抽象类在开放源代码库中,因此您不知道可以从中扩展整个宇宙中的哪些其他类。)现在,您发现向该类添加新的抽象方法很有用。但是,如果将抽象方法添加到该类,则会破坏已经对其进行扩展的所有其他类,因为需要更改这些类以包含新方法的替代。因此,我可以看到,在某些情况下,将它们添加为非抽象方法并让它们引发异常可能更可行,以防万一扩展了它的 new 类使用新方法,但是忘记编写重写方法。它不会在编译时捕获,但是至少在测试过程中可能会捕获。

I can think of one (possibly) valid use case: You have an abstract class, and a number of other classes throughout your project that extended it. (Or the abstract class is in an open-source library, so you have no idea what other classes throughout the universe might be extended from it.) Now, you find that it's useful to add new abstract methods to this class. But if you add abstract methods to the class, this breaks every other class that has already extended it, because those classes need to be changed to contain override for the new methods. So I can see how, in some cases, it might be considered more feasible to add them as non-abstract methods and have them throw exceptions just in case a new class that extends it uses the new methods but forgets to write an overriding method. It won't be caught at compile-time, but at least it might be caught during testing.

我不确定这个原因是否合理,因为也可以定义一个新的抽象类 NewImprovedWhatever 扩展了旧方法,以包含新方法。但是,这可能会带来自身的维护挑战。我不确定。

I'm not sure how legitimate this reason is, since it's also possible to define a new abstract class NewImprovedWhatever that extends the old one, to contain the new methods. That may pose its own maintenance challenges, though. I'm not sure.

我确实注意到Java定义了一个抽象类 java.net.SocketImpl ,包含两个非抽象方法 shutdownInput() shutdownOutput(),它们的默认实现是引发异常:

I did notice that Java defines an abstract class java.net.SocketImpl, that contains two non-abstract methods, shutdownInput() and shutdownOutput(), whose default implementation is to throw an exception:

protected void shutdownInput() throws IOException {
  throw new IOException("Method not implemented!");
}

注释表明它们已添加到1.3中,所以也许这就是确切的原因这是通过这种方式完成的,即不需要修改已经扩展了 SocketImpl 的其他类。但是我不确定。

The comments indicate that they were added in 1.3, so perhaps that's the exact reason it was done this way, i.e. not to require modification of other classes that already extended SocketImpl. But I don't know for sure.

无论如何,如果该类是从头开始设计的,那么这样的设计肯定是不好的代码。但是,当我们需要修改现有代码时,我们在课堂上学到的O-O概念并不总是那么好。

In any case, a design like this would surely be really bad code, if the class were designed that way from scratch. But the nice O-O concepts we learn about in class aren't always so nice in practice when existing code has to be modified.

这篇关于使用Java中的异常伪造抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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