关于“二进制兼容性”的问题被称为“二进制兼容性”。 [英] Issue about "binary compatibility"

查看:215
本文介绍了关于“二进制兼容性”的问题被称为“二进制兼容性”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读Java语言规范(JLS8)> 二进制兼容性,其中一项不会破坏二进制兼容性的更改是:

When I read Java Language Specification (JLS8) > Binary Compatibility, one of a set changes that doesn't break binary compatibility is:


更改方法或构造函数返回先前输入
抛出异常的输入,这些异常通常不会发生或由于
进入无限循环或导致死锁而失败或失败

Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur or failed by going into an infinite loop or causing a deadlock

我不明白这个主意。

请帮助澄清并举例说明。

Please help clarify and give an example to demonstrate it.

推荐答案


更改方法或构造函数以返回其先前曾为
抛出异常的输入值

Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur

现有代码:

public int square(int x) {
    if (x % 2 == 0) {
        return x * x;
    } else {
        throw new IllegalArgumentException("Cannot square odd numbers");
    }
}

满足上述规则的兼容更改示例:

Example of a compatible change that satisfies above rule:

public int square(int x) {
    return x * x;
}

不兼容更改的示例:

public int square(int x) {
    if (x % 2 == 1) {
        return x * x;
    } else {
        throw new IllegalArgumentException("Cannot square even numbers");
    }
}




或失败进入无限循环或导致死锁

or failed by going into an infinite loop or causing a deadlock

之前:

public int square(int x) {
    if (x % 2 == 0) {
        return x * x;
    } else {
        while (true)
            ;
    }
}

满足上述规则的兼容更改示例:

Example of a compatible change that satisfies above rule:

public int square(int x) {
    if (x % 2 == 0) {
        return x * x;
    } else {
        // Even this is binary compatible (although bad form as the method
        // name is no longer self-explanatory.)
        return x * x * x;
    }
}

我想您明白了。

语句的实际含义是:


  • 您可以添加使您的方法成功完成之前无法完成的工作的功能

  • 但是您不能更改已经有效的输入的现有行为,同时保持二进制兼容。

  • You can add functionality that makes your method do something it couldn't do before successfully
  • But you cannot change existing behaviour for already-valid inputs while remaining binary compatible.

这是一种表达很多常识的奇特方式。

It's a fancy way of saying something that make a lot of common sense.

这篇关于关于“二进制兼容性”的问题被称为“二进制兼容性”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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