📜  系统引用Java的软件系统中的向后兼容性

📅  最后修改于: 2022-05-13 01:55:00.923000             🧑  作者: Mango

系统引用Java的软件系统中的向后兼容性

在这里,我们将讨论在任何软件系统中开发新功能的同时实现“向后兼容”的方法。因此,让我们简要介绍一下向后兼容性。向后兼容性是系统、产品或技术的一种属性,它允许与较旧的遗留系统或为此类系统设计的输入集成。以不允许向后兼容的方式修改系统称为“重大变更”。虽然当前的敏捷软件开发时代需要快节奏的功能实现以满足快速变化的需求,但在开发的软件中具有向后兼容性是非常必要的。

在这里,我们将讨论Java如何在开发新功能的同时实现向后兼容,这将作为更新现有代码时的潜在参考。

现在让我们了解“加宽”这个非常重要的概念,以及在开发Java 5 中的自动装箱等新特性时如何保留它。对重载方法的调用。

示例 1:

Java
// Java Program to Without Involving Concepts 
// of Java 5 Features
  
// Importing required classes
import java.io.*;
  
// Main class
// To test method overloading
class GFG {
  
    // Method 1
    static void print(int i)
    {
        // Print statement when method 1 is called
        System.out.println("int = " + i);
    }
  
    // Method 2
    static void print(long l)
    {
        // Print statement when method 2 is called
        System.out.println("long = " + l);
    }
  
    // Method 3
    static void print(double d)
    {
        // Print statement when method 3 is called
        System.out.println("double = " + d);
    }
  
    // Method 4
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variables
        byte b = 5;
        short s = 5;
        long l = 5;
        float f = 5.0f;
  
        // Calling the methods
        print(b);
        print(s);
        print(l);
        print(f);
    }
}


Java
// Main class
// To test Method Overloading
class TestMethodOverloading {
      
    static void print(double d)
    {
        System.out.println("double = " + d);
    }
    public static void main(String[] args)
    {
        byte b = 5;
        short s = 5;
        long l = 5;
        float f = 5.0f;
        print(b);
        print(s);
        print(l);
        print(f);
    }
}


Java
// Java Program Illustrating Maintainence of Backward
// Compatibility
  
// Importing required classes
import java.io.*;
  
// Main class
// To test backwards compatibility
class GFG {
  
    // Method 1
    static void print(Integer i)
    {
        // Print statement whenever method 1 is called
        System.out.println("Integer = " + i);
    }
  
    // Method 2
    static void print(long l)
    {
        // Print statement whenever method 1 is called
        System.out.println("long = " + l);
    }
  
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        int i = 5;
  
        // Calling method over above custom input
        print(i);
    }
}


输出

int = 5
int = 5
long = 5
double = 5.0

当使用byte参数调用方法时, Java编译器会隐式扩展它以匹配接受 int 参数的适当版本的print()方法。此外,可以看出,当使用long调用打印方法时,它使用相应版本的打印方法print(long l),并且使用浮点数的方法调用与采用精度值的方法匹配,因此,在转,解释加宽的行为。

示例 2:这里我们将只有一个接受double作为参数的方法。当使用byte, short, long调用时, float ,它将匹配print()方法的所有四个调用

Java

// Main class
// To test Method Overloading
class TestMethodOverloading {
      
    static void print(double d)
    {
        System.out.println("double = " + d);
    }
    public static void main(String[] args)
    {
        byte b = 5;
        short s = 5;
        long l = 5;
        float f = 5.0f;
        print(b);
        print(s);
        print(l);
        print(f);
    }
}
输出
double = 5.0
double = 5.0
double = 5.0
double = 5.0

现在让我们讨论一下 Java5 中的自动装箱功能。自动装箱是Java 5 中引入的关键特性之一。自动装箱是Java编译器在原始类型与其对应的对象包装类之间进行的自动转换。例如,将 int 转换为 Integer,将 double 转换为 Double,等等。因此,请发挥Java 5 向后兼容性的概念。参考自动装箱,请考虑以下示例,它将帮助我们了解Java如何在引入新功能的同时保持向后兼容性。

例子

Java

// Java Program Illustrating Maintainence of Backward
// Compatibility
  
// Importing required classes
import java.io.*;
  
// Main class
// To test backwards compatibility
class GFG {
  
    // Method 1
    static void print(Integer i)
    {
        // Print statement whenever method 1 is called
        System.out.println("Integer = " + i);
    }
  
    // Method 2
    static void print(long l)
    {
        // Print statement whenever method 1 is called
        System.out.println("long = " + l);
    }
  
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        int i = 5;
  
        // Calling method over above custom input
        print(i);
    }
}
输出
long = 5

从上述输出中学习如下:

  • 如果print()方法的唯一版本接受Integer ,那么参考Java 5 的自动装箱功能, print() 的调用将成功。同样,如果只有长版本存在,编译器也会调用相同的版本。
  • 如果两种方法都存在,会使用哪一种呢?所以为了建立 backwoods 兼容性,编译器认为加宽原始参数比执行自动装箱操作更可取,所以编译器会选择加宽而不是装箱,输出将是

结论:在本文中,我们通过示例了解了在开发新功能的同时如何维护和考虑实现“向后兼容”。 Java的设计者给予优先考虑现有的功能“加宽”在Java 5的一个重要规则出台新的“自动装箱”的特点就是既有代码的函数应该使用它的方式,因此编译器将始终选择老式它选择之前较新的样式(扩展自动装箱)。