package-info.java的作用

在阅读一些开源的Java Source Code时,我们通常会发现在每个package的下面,会有一个这样的文件:

package-info.java

乍看之下,它并不符合Java类的命名规范。在Eclipse是无法直接创建这个类的,可以先创建一个其它名字的类,再rename回来。

而且打开这个文件,通常会发现里面没有类申明,只有这个包的申明,和包上的Javadoc注释,当然,包级别的Annotation也会使用在这个类里面。

如果这个类不包含包级别的Annotation,是没有必要一定需要这个名字的,如果有包级别的Annotation,Eclipse会提示“Package annotations must be in file package-info.java”

其实,通常这个文件有三个作用:

1.为标注在包上Annotation提供便利

2.申明包级别的私有类和变量

3.提供包级别的整体注释

java电子书免费下载


以下是示例:

 

Java代码 复制代码  收藏代码
  1. package pkg.info;   
  2.     
  3. import java.lang.annotation.ElementType;   
  4. import java.lang.annotation.Retention;   
  5. import java.lang.annotation.RetentionPolicy;   
  6. import java.lang.annotation.Target;   
  7.     
  8. @Target(ElementType.PACKAGE)      
  9. @Retention(RetentionPolicy.RUNTIME)    
  10. public @interface PkgAnnotation {   
  11.     
  12. }  
package pkg.info;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(ElementType.PACKAGE)   
@Retention(RetentionPolicy.RUNTIME) 
public @interface PkgAnnotation {
 
}
 

 

Java代码 复制代码  收藏代码
  1. /**  
  2.  *     
  3.  * <b>package-info has three function</b><br>    
  4.    
  5.  * 1.package annotation<br>    
  6.  * 2.package level class and constant<br>    
  7.  * 3.package level comment<br>     
  8. */    
  9.     
  10.  //这个文件是无法声明公共类的,否则类名必须与文件名相同。   
  11. @PkgAnnotation  
  12. package pkg.info;   
  13. //包级别的私有类   
  14. class Test{   
  15.     public void sayHi(){   
  16.         System.out.println("Hi");   
  17.     }   
  18. }   
  19. //定义包级别的常量   
  20. class Constant{   
  21.     public static final String NAME = "abc";   
  22. }   
  23.     
  24.   
  25.   
  26. package pkg.info;   
  27.     
  28. import java.lang.annotation.Annotation;   
  29.     
  30. public class Client {   
  31.     public static void main(String[] args) {   
  32.     
  33.         Annotation[] annotations = Client.class.getPackage().getAnnotations();   
  34.     
  35.         for (Annotation an : annotations) {   
  36.             if (an instanceof PkgAnnotation) {   
  37.                 System.out.println("This is pkg annotation");   
  38.             }   
  39.         }   
  40.         System.out.println(Constant.NAME);   
  41.         Test test = new Test();   
  42.         test.sayHi();   
  43.     }   
  44.     
  45. }  
/**
 *   
 * <b>package-info has three function</b><br>  
 
 * 1.package annotation<br>  
 * 2.package level class and constant<br>  
 * 3.package level comment<br>   
*/ 
 
 //这个文件是无法声明公共类的,否则类名必须与文件名相同。
@PkgAnnotation
package pkg.info;
//包级别的私有类
class Test{
    public void sayHi(){
        System.out.println("Hi");
    }
}
//定义包级别的常量
class Constant{
    public static final String NAME = "abc";
}
 


package pkg.info;
 
import java.lang.annotation.Annotation;
 
public class Client {
    public static void main(String[] args) {
 
        Annotation[] annotations = Client.class.getPackage().getAnnotations();
 
        for (Annotation an : annotations) {
            if (an instanceof PkgAnnotation) {
                System.out.println("This is pkg annotation");
            }
        }
        System.out.println(Constant.NAME);
        Test test = new Test();
        test.sayHi();
    }
 
}

  这个包在运行JavaDoc后会产生如下结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值