什么是Java中的二进制兼容性? [英] What is binary compatibility in Java?

查看:369
本文介绍了什么是Java中的二进制兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Joshua Bloch的 Effective Java

I was reading Effective Java by Joshua Bloch.

在第17项:仅使用接口定义类型中,我遇到了不建议使用接口存储常量的解释。我在下面给出解释。

In Item 17: "Use interfaces only to define types", I came across the explanation where it is not advised to use Interfaces for storing constants. I am putting the explanation below.

更糟糕的是,它代表了一个承诺:如果在将来的版本中修改了类,那么它就是
不再需要使用常量,它仍然必须实现接口以确保二进制
兼容性。

"Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility."

什么二进制兼容性在这里意味着什么?

What does binary compatibility mean here?

有人可以用Java中的示例指导我,以证明代码是二进制兼容的。

Can someone guide me with an example in Java to show that code is binary compatible.

推荐答案

简而言之,二进制兼容性意味着当您更改类时,不需要重新编译使用它的类。例如,您从此类中删除或重命名了公共或受保护的方法

In short, binary compatibility means that when you change your class, you do not need to recompile classes that use it. For example, you removed or renamed a public or protected method from this class

public class Logger implements Constants {
   public Logger getLogger(String name) {
         return LogManager.getLogger(name);
   }
}

并发布了一个新版本为log-2.jar。当您的log-1.jar用户下载新版本时,当他们尝试使用缺少的getLogger(String name)方法时,它将破坏他们的应用程序。

from your log-1.jar library and released a new version as log-2.jar. When users of your log-1.jar download the new version it will break their apps when they will try to use the missing getLogger(String name) method.

如果删除常量接口(第17项),由于相同的原因,这将破坏二进制兼容性。

And if you remove Constants interface (Item 17) this will break binary compatibility either, due to the same reason.

但是您可以删除/重命名此类的私有或包私有成员,而不会破坏二进制兼容性,因为外部应用程序不能(或不应该)使用它。

But you can remove / rename a private or package private member of this class without breaking the binary compatibility, because external apps cannot (or should not) use it.

这篇关于什么是Java中的二进制兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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