當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.lang.Package用法及代碼示例


Java 2 添加了一個名為 Package 的類,它封裝了與包關聯的版本數據。由於包的激增以及 java 程序可能需要知道可用的包版本,包版本信息變得越來越重要。
此版本控製信息由加載類的 ClassLoader 實例檢索並提供。通常,它存儲在與類一起分發的清單中。它擴展了 Object 類並實現了 AnnotatedElement。

方法:

  1. getAnnotation(類注釋類):如果存在指定類型的該元素的注釋,則返回該元素的注釋,否則返回 null。
    Syntax: public  A getAnnotation(Class annotationClass)
    Returns: this element's annotation for the specified 
    annotation type if present on this element, else null.
    Exception: NullPointerException - if the given annotation class is null.
    
    
    // Java code illustrating getAnnotation() method  
    import java.lang.annotation.Retention; 
    import java.lang.annotation.RetentionPolicy; 
    import java.lang.reflect.Method; 
      
    // declare a annotation type 
    @Retention(RetentionPolicy.RUNTIME) 
    @interface Demo  
    { 
       String str(); 
       int val(); 
    } 
      
    public class PackageDemo  
    { 
      
       // setting values for the annotation 
       @Demo(str = " Gfg Demo Annotation", val = 100) 
         
       // a method to call in the main 
       public static void gfg() throws NoSuchMethodException  
       { 
          PackageDemo ob = new PackageDemo(); 
      
            
             Class c = ob.getClass(); 
      
             // get the method example 
             Method m = c.getMethod("gfg"); 
      
             // get the annotation for class Demo 
             Demo annotation = m.getAnnotation(Demo.class); 
      
             // checking the annotation 
             System.out.println(annotation.str() + " " + annotation.val()); 
        }  
         
       public static void main(String args[]) throws Exception 
       { 
          gfg(); 
       } 
    } 

    輸出:

    Gfg Demo Annotation 100
    
  2. 注釋[]getAnnotations():返回此元素上存在的所有注釋。 (如果此元素沒有注釋,則返回長度為零的數組。)此方法的調用者可以自由修改返回的數組;它不會影響返回給其他調用者的數組。
    Syntax: public Annotation[] getDeclaredAnnotations().
    Returns: All annotations directly present on this element.
    Exception: NA.
    
    
    // Java code illustrating getAnnotation() method 
    import java.lang.annotation.Annotation; 
    import java.lang.annotation.Retention; 
    import java.lang.annotation.RetentionPolicy; 
    import java.lang.reflect.Method; 
      
          
    // declare a annotation type 
    @Retention(RetentionPolicy.RUNTIME) 
    @interface Demo  
    { 
       String str(); 
       int val(); 
    } 
      
    public class PackageDemo  
    { 
      
       // setting values for the annotation 
       @Demo(str = " Gfg Demo Annotation", val = 100) 
         
       // a method to call in the main 
       public static void gfg() throws NoSuchMethodException  
       { 
          PackageDemo ob = new PackageDemo(); 
      
            
             Class c = ob.getClass(); 
      
             // get the method example 
             Method m = c.getMethod("gfg"); 
      
             // get the annotation for class Demo 
             Demo annotation = m.getAnnotation(Demo.class); 
      
             // checking the annotation 
             System.out.println(annotation.str() + " " + annotation.val()); 
             Annotation[] gfg_ann = m.getAnnotations(); 
               
             for(int i = 0; i < gfg_ann.length; i++) 
             { 
                 System.out.println(gfg_ann[i]); 
             } 
        }  
         
       public static void main(String args[]) throws Exception 
       { 
          gfg(); 
       } 
    } 

    輸出:

    Gfg Demo Annotation 100
    @Demo(str= Gfg Demo Annotation, val=100)
    
  3. 注釋[]getDeclaredAnnotations():返回直接出現在該元素上的所有注釋。與此接口中的其他方法不同,此方法忽略繼承的注釋。 (如果此元素上沒有直接存在注釋,則返回長度為零的數組。)此方法的調用者可以自由修改返回的數組;它不會影響返回給其他調用者的數組。
    Syntax: public Annotation[] getDeclaredAnnotations().
    Returns: All annotations directly present on this element.
    Exception: NA.
    
    
    // java code illustrating getDeclaredAnnotation() method 
    import java.lang.annotation.Annotation; 
    import java.lang.annotation.Retention; 
    import java.lang.annotation.RetentionPolicy; 
    import java.lang.reflect.Method; 
      
    // declare a annotation type 
    @Retention(RetentionPolicy.RUNTIME) 
    @interface Demo  
    { 
       String str(); 
       int val(); 
    } 
      
    public class PackageDemo  
    { 
      
       // setting values for the annotation 
       @Demo(str = " Gfg Demo Annotation", val = 100) 
         
       // a method to call in the main 
       public static void gfg() throws NoSuchMethodException  
       { 
          PackageDemo ob = new PackageDemo(); 
      
            
             Class c = ob.getClass(); 
      
             // get the method example 
             Method m = c.getMethod("gfg"); 
      
             // get the annotation for class Demo 
             Demo annotation = m.getAnnotation(Demo.class); 
      
             // checking the annotation 
             System.out.println(annotation.str() + " " + annotation.val()); 
             Annotation[] gfg_ann = m.getDeclaredAnnotations(); 
               
             for(int i = 0; i < gfg_ann.length; i++) 
             { 
                 System.out.println(gfg_ann[i]); 
             } 
        }  
         
       public static void main(String args[]) throws Exception 
       { 
          gfg(); 
       } 
    } 

    輸出:

     Gfg Demo Annotation 100
    @Demo(str= Gfg Demo Annotation, val=100)
    
  4. 字符串getImplementationTitle():返回此包的標題。
    Syntax: public String getImplementationTitle()
    Returns: the title of the implementation, null is returned if it is not known.
    Exception: NA
    
  5. 字符串getImplementationVersion():返回此實現的版本。它由該實現的供應商分配的任何字符串組成,並且沒有 Java 運行時指定或期望的任何特定語法。可以將其與該供應商針對該包的該實現所使用的其他包版本字符串進行比較。
    Syntax: public String getImplementationVersion()
    Returns: the version of the implementation, null is returned if it is not known.
    Exception: NA
    
  6. 字符串getImplementationVendor():返回提供此實現的組織、供應商或公司的名稱。
    Syntax: public String getImplementationVendor().
    Returns: the vendor that implemented this package.
    Exception: NA.
    
  7. 字符串getName():返回此包的名稱。
    Syntax: public String getName()
    Returns: The fully-qualified name of this package as defined
     in section 6.5.3 of The Java™ Language Specification, for example, java.lang.
    Exception: NA
    
    
    // Java code illustrating getName(), getImplementationTitle() 
    // and getImplementationVendor() and getImplementationVersion() 
    // methods 
    class PackageDemo 
    { 
        public static void main(String arg[]) 
        { 
            Package pkgs[]; 
            pkgs = Package.getPackages(); 
              
            for(int i=0; i<1; i++) 
            { 
                //  name of the package 
                System.out.println(pkgs[i].getName()); 
                  
                // checking title of this implementation 
                System.out.println(pkgs[i].getImplementationTitle()); 
                  
                // checking the vendor 
                System.out.println(pkgs[i].getImplementationVendor()); 
                  
                // version of this implementation 
                System.out.println(pkgs[i].getImplementationVersion()); 
                              
            } 
        } 
    } 

    輸出:

    sun.reflect
    Java Runtime Environment
    Oracle Corporation
    1.8.0_121
    
  8. 靜態包 getPackage(字符串名稱):在調用者 ClassLoader 實例中按名稱查找包。調用者ClassLoader實例用於查找與指定類對應的包實例。如果調用者 ClassLoader 實例為空,則搜索係統 ClassLoader 實例加載的包集以查找指定的包。
    Syntax: public static Package getPackage(String name)
    Returns: the package of the requested name. It may 
    be null if no package information is available from the archive or 
    codebase.
    Exception: NA
    
  9. 靜態包[]getPackages():獲取調用者的 ClassLoader 實例當前已知的所有包。這些包對應於通過 ClassLoader 實例加載或通過名稱訪問的類。如果調用者的 ClassLoader 實例是 bootstrap ClassLoader 實例(在某些實現中可能用 null 表示),則隻會返回與 bootstrap ClassLoader 實例加載的類對應的包。
    Syntax: public static Package[] getPackages()
    Returns: a new array of packages known to the callers 
    ClassLoader instance. An zero length array is returned if none are known.
    Exception: NA
    
    
    // Java code illustrating getPackages() method 
    class PackageDemo 
    { 
        public static void main(String arg[]) 
        { 
            Package pkgs[]; 
            pkgs = Package.getPackages(); 
              
            Package pkg = Package.getPackage("java.lang"); 
              
            for(int i=0; i<1; i++) 
            { 
            System.out.println(pkg.getName()); 
            System.out.println(pkgs[i].getName()); 
            } 
        } 
    } 

    輸出:

    java.lang
    sun.reflect
    
  10. 字符串getSpecificationTitle():返回此包實現的規範的標題。
    Syntax: public String getSpecificationTitle()
    Returns: the specification title, null is returned 
    if it is not known.
    exception: NA.
    
  11. 字符串getSpecificationVersion():返回此包實現的規範的版本號。此版本字符串必須是由 “.” 分隔的非負十進製整數序列,並且可以有前導零。比較版本字符串時,將比較最重要的數字。
    Syntax: public String getSpecificationVersion().
    Returns: the specification version, null is returned 
    if it is not known.
    Exception: NA.
    
  12. 字符串getSpecificationVendor():返回擁有並維護實現此包的類的規範的組織、供應商或公司的名稱。
    Syntax: public String getSpecificationVendor()
    Returns: the specification vendor, null is returned
     if it is not known.
    Exception: NA.
    
  13. int hashCode():返回根據包名稱計算的哈希碼。
    Syntax: Return the hash code computed from the package name.
    Exception: NA
    Returns: the hash code.
    
    
    // Java code illustrating hashCode(), getSpecificationTitle() 
    // getSpecificationVendor() and getSpecificationVersion() 
    class PackageDemo 
    { 
        public static void main(String arg[]) 
        { 
            Package pkgs[]; 
            pkgs = Package.getPackages(); 
              
            for(int i=0; i<1; i++) 
            { 
                //  name of the package 
                System.out.println(pkgs[i].hashCode()); 
                  
                // checking title  
                System.out.println(pkgs[i].getSpecificationTitle()); 
                  
                // checking the vendor 
                System.out.println(pkgs[i].getSpecificationVendor()); 
                  
                // checking version 
                System.out.println(pkgs[i].getSpecificationVersion()); 
                              
            } 
        } 
    } 

    輸出:

    685414683
    Java Platform API Specification
    Oracle Corporation
    1.8
    
  14. 布爾 isCompatibleWith(所需字符串):將此軟件包的規範版本與所需版本進行比較。如果此包規範版本號大於或等於所需的版本號,則返回 true。
    Syntax: public boolean isCompatibleWith(String desired).
    Returns: true if this package's version number is 
    greater than or equal to the desired version number
    Exception: 
    NumberFormatException - if the desired or current version is not 
    of the correct dotted form.
    
  15. 布爾值isSealed():如果此包裝已密封,則返回 true。
    Syntax: public boolean isSealed()
    Returns: true if the package is sealed, false otherwise.
    Exception: NA
    
  16. 布爾 isSealed(URL url):如果此包相對於指定的代碼源 url 是密封的,則返回 true。
    Syntax: public boolean isSealed(URL url)
    Returns: true if this package is sealed with respect to url
    Exception: NA
    
  17. 字符串toString():返回此包的字符串表示形式。它的值是字符串“package”和包名稱。如果定義了包標題,則會附加它。如果定義了包版本,則會附加它。
    Syntax: public String toString()
    Returns: the string representation of the package.
    Exception: NA
    
    
    // java code illustrating isCompatibleWith(), toString(), 
    // isSealed methods 
      
    import java.net.MalformedURLException; 
    import java.net.URL; 
      
    class PackageDemo 
    { 
        public static void main(String arg[]) throws MalformedURLException 
        { 
            Package pkg = Package.getPackage("java.lang"); 
              
            // checking if pkg is compatible with 1.0 
            System.out.println(pkg.isCompatibleWith("1.0")); 
              
            // checking if packet is sealed 
            System.out.println(pkg.isSealed()); 
              
            URL url = new URL("https://www.youtube.com/"); 
              
            System.out.println(pkg.isSealed(url)); 
              
            // string equivalent of package 
            System.out.println(pkg.toString()); 
              
        } 
    } 

    輸出:

    true
    false
    false
    package java.lang, Java Platform API Specification, version 1.8
    


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.lang.Package class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。