📜  用示例在Java中封装 isCompatibleWith() 方法

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

用示例在Java中封装 isCompatibleWith() 方法

Java.lang.Package 类isCompatibleWith()方法用于检查该包的规范版本是否与指定版本兼容。该方法将结果作为布尔值返回。
句法:

public boolean isCompatibleWith(String desiredVersion)

参数:此方法接受参数desiredVersion ,该参数是要检查兼容性的版本。
返回值:此方法将结果作为布尔值返回。
下面的程序演示了 isCompatibleWith() 方法。
示例 1:

Java
// Java program to demonstrate
// isCompatibleWith() method
 
public class Test {
    public static void main(String[] args)
    {
 
        // returns the Package
        // object for this package
        Package myPackage
            = Package.getPackage("java.lang");
 
        System.out.println(
            "Package represented by myPackage: "
            + myPackage.toString());
 
        String desiredVersion = "1.5";
 
        // check if this package is compatible with or not
        // using isCompatibleWith() method
        System.out.println(
            "Is this package compatible with or not: "
            + myPackage.isCompatibleWith(desiredVersion));
    }
}


Java
// Java program to demonstrate
// isCompatibleWith() method
 
public class Test {
    public static void main(String[] args)
    {
 
        // returns the Package
        // object for this package
        Package myPackage
            = Package.getPackage("java.io");
 
        System.out.println(
            "Package represented by myPackage: "
            + myPackage.toString());
 
        String desiredVersion = "1.0";
 
        // check if this package is compatible with or not
        // using isCompatibleWith() method
        System.out.println(
            "Is this package compatible with or not: "
            + myPackage.isCompatibleWith(desiredVersion));
    }
}


输出:
Package represented by myPackage: package java.lang, Java Platform API Specification, version 1.8
Is this package compatible with or not: true

示例 2:

Java

// Java program to demonstrate
// isCompatibleWith() method
 
public class Test {
    public static void main(String[] args)
    {
 
        // returns the Package
        // object for this package
        Package myPackage
            = Package.getPackage("java.io");
 
        System.out.println(
            "Package represented by myPackage: "
            + myPackage.toString());
 
        String desiredVersion = "1.0";
 
        // check if this package is compatible with or not
        // using isCompatibleWith() method
        System.out.println(
            "Is this package compatible with or not: "
            + myPackage.isCompatibleWith(desiredVersion));
    }
}
输出:
Package represented by myPackage: package java.io, Java Platform API Specification, version 1.8
Is this package compatible with or not: true

参考: https://docs.oracle.com/javase/9/docs/api/ Java/lang/Package.html#isCompatibleWith-java.lang.String-