博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Java中有类似.NET的NotImplementedException吗?
阅读量:2289 次
发布时间:2019-05-09

本文共 4044 字,大约阅读时间需要 13 分钟。

本文翻译自:

在Java中有类似.NET的NotImplementedException吗?


#1楼

参考:


#2楼

No there isn't and it's probably not there, because there are very few valid uses for it. 没有,它可能不存在,因为它的有效用途很少。 I would think twice before using it. 在使用之前我会三思。 Also, it is indeed easy to create yourself. 而且,创建自己确实很容易。

Please refer to about why it's even in .NET. 请参阅了解为什么它甚至在.NET中。

I guess UnsupportedOperationException comes close, although it doesn't say the operation is just not implemented, but unsupported even. 我猜UnsupportedOperationException接近,虽然它没有说操作没有实现,但是甚至不支持。 That could imply no valid implementation is possible. 这可能意味着无法实现有效的实施。 Why would the operation be unsupported? 为什么不支持该操作? Should it even be there? 它应该在那吗? Interface segregation or Liskov substitution issues maybe? 接口隔离或Liskov替换问题可能?

If it's work in progress I'd go for ToBeImplementedException , but I've never caught myself defining a concrete method and then leave it for so long it makes it into production and there would be a need for such an exception. 如果它正在进行中,我会选择ToBeImplementedException ,但是我从未发现自己定义了一个具体的方法,然后将它保留了很长时间才使其投入生产,并且需要这样的异常。


#3楼

我认为你正在寻找 。


#4楼

has it. 有它。 Or you could throw an . 或者您可以抛出 。


#5楼

As mentioned, the JDK does not have a close match. 如前所述,JDK没有紧密匹配。 However, my team occasionally has a use for such an exception as well. 但是,我的团队偶尔也会使用这种例外。 We could have gone with UnsupportedOperationException as suggested by other answers, but we prefer a custom exception class in our base library that has deprecated constructors: 我们可以按照其他答案的建议使用UnsupportedOperationException ,但我们更喜欢在我们的基础库中使用已弃用的构造函数的自定义异常类:

public class NotYetImplementedException extends RuntimeException{    /**     * @deprecated Deprecated to remind you to implement the corresponding code     *             before releasing the software.     */    @Deprecated    public NotYetImplementedException()    {    }    /**     * @deprecated Deprecated to remind you to implement the corresponding code     *             before releasing the software.     */    @Deprecated    public NotYetImplementedException(String message)    {        super(message);    }}

This approach has the following benefits: 这种方法有以下好处:

  1. When readers see NotYetImplementedException , they know that an implementation was planned and was either forgotten or is still in progress, whereas UnsupportedOperationException says (in line with ) that something will never be implemented. 当读者看到NotYetImplementedException ,他们知道某个实现已被计划并且被遗忘或仍在进行中,而UnsupportedOperationException表示(与 )将永远不会实现某些内容。 That's why we have the word "yet" in the class name. 这就是为什么我们在班级名称中有“尚未”这个词。 Also, an IDE can easily list the call sites. 此外,IDE可以轻松列出呼叫站点。
  2. With the deprecation warning at each call site, your IDE and static code analysis tool can remind you where you still have to implement something. 通过每个调用站点的弃用警告,您的IDE和静态代码分析工具可以提醒您仍需要实现某些功能。 (This use of deprecation may feel wrong to some, but in fact .) (对于某些人来说,这种弃用可能会让人觉得不对,但事实上, 。)
  3. The constructors are deprecated, not the class. 不推荐使用构造函数,而不是类。 This way, you only get a deprecation warning inside the method that needs implementing, not at the import line (JDK 9 , though). 这样,您只需要在需要实现的方法中获得弃用警告,而不是在import行(JDK 9 )。

#6楼

You could do it yourself (thats what I did) - in order to not be bothered with exception handling, you simply extend the RuntimeException, your class could look something like this: 你可以自己做(这就是我所做的) - 为了不被异常处理所困扰,你只需要扩展RuntimeException,你的类看起来像这样:

public class NotImplementedException extends RuntimeException {    private static final long serialVersionUID = 1L;    public NotImplementedException(){}}

You could extend it to take a message - but if you use the method as I do (that is, as a reminder, that there is still something to be implemented), then usually there is no need for additional messages. 您可以扩展它以接收消息 - 但如果您像我一样使用该方法(即,作为提醒,仍有一些东西需要实现),那么通常不需要其他消息。

I dare say, that I only use this method, while I am in the process of developing a system, makes it easier for me to not lose track of which methods are still not implemented properly :) 我敢说,我只使用这种方法,而我正在开发一个系统,让我更容易忘记哪些方法仍未正确实施:)

转载地址:http://cscnb.baihongyu.com/

你可能感兴趣的文章
MySQL group by的sql_mode报错
查看>>
联合索引应用细节
查看>>
Nginx地址重定向 return rewrite if
查看>>
PHP安装Redis模块
查看>>
PHP追加安装时候忘装的模块
查看>>
PHP平滑升级
查看>>
MySQL删除无用账户
查看>>
MySQL多实例配置方案
查看>>
MySQL设置及修改root密码
查看>>
MySQL忘记密码重置管理员密码
查看>>
MySQL创建及授权账号
查看>>
MySQL库的基本操作
查看>>
MySQL表的基本操作
查看>>
MySQL数据类型
查看>>
MySQL SQL语句最常见的分类
查看>>
MySQL用户权限
查看>>
MySQL数据备份
查看>>
MySQL使用explain检查索引执行计划
查看>>
MySQL字符集
查看>>
MySQL存储引擎
查看>>