用户操作
[即时聊天] [发私信] [加为好友]
曾巧ID:numenZQ
42206次访问,排名2821,好友0人,关注者0人。
numenZQ的文章
原创 29 篇
翻译 16 篇
转载 2 篇
评论 16 篇
最近评论
xh:不推荐修改web.xml,设置java_options更好
numenZQ:补充说明一点,使用java.util.zip包时,是以UTF-8编码格式读取的文件名,因此在中文windows操作系统(Windows操作系统默认字符集为:GBK)中使用时会导致文件名解析错误,因此需要使用org.apache.tools.zip.ZipEntry和 org.apache.tools.zip.ZipOutputStream类来解决这一问题。
numenZQ:这个是需要明确知道字符串的成分,该方法只是为了满足读取不同字符集相应字符串,还是以“多哈亚运会”为例:如果字符集为GBK,截取前6个字节,结果为:“多哈亚”;当字符集为UTF-8时,截取前6个字节,结果则为:“多哈”,这是因为GBK是双字节编码,而UTF-8是三字节变长编码,如果不分字符集来读取对应长度的字串,则会出现字串内容与预期不符,长度错误等问题。
lyazure:仔细看了你的代码,作用是从一个字符串中获取指定字节数的字符,不知道你要这么做的最终目的是用来做什么。除非明确知道字符串的成分,否则这种做法很难做到完美,比如你的代码中,假如出现要从“多哈亚运会”这样的字符串中截取7个字节,最终会截得3个字符。
Alexandre:ab8e44bc75204d49bf0c9fe68a2b2176 matura foto amatoriale
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Java Annotation(3)收藏

    新一篇: Java Annotation(4) | 旧一篇: Java Annotation(2)

    作者:曾巧(numenzq

     

    摘要

    在之前的文章中,我们已经了解了Annotation的基本概念,Java的内置Annotation和如何定制自己的Annotation;在这里我们将学习Annotation的另一个特性:元注释,它可以使我们更好的定制我们的注释。

     

    内容

    l         限定注释使用范围

    l         注释保持性策略

    l         文档化功能

    l         标注继承

    l         概要

     

    限定注释使用范围

        当我们的自定义注释不断的增多也比较复杂时,就会导致有些开发人员使用错误,主要表现在不该使用该注释的地方使用。为此,Java提供了一个ElementType枚举类型来控制每个注释的使用范围,比如说某些注释只能用于普通方法,而不能用于构造函数等。下面是Java定义的ElementType枚举:

    package java.lang.annotation;

     

    public enum ElementType {

      TYPE,         // Class, interface, or enum (but not annotation)

      FIELD,        // Field (including enumerated values)

      METHOD,       // Method (does not include constructors)

      PARAMETER,        // Method parameter

      CONSTRUCTOR,      // Constructor

      LOCAL_VARIABLE,   // Local variable or catch clause

      ANNOTATION_TYPE,  // Annotation Types (meta-annotations)

      PACKAGE       // Java package

    }

        下面我们来修改Greeting注释,为之添加限定范围的语句,这里我们称它为目标(Target)使用方法也很简单,如下:

    package com.gelc.annotation.demo.customize;

     

    @Target( { ElementType.METHOD, ElementType.CONSTRUCTOR })

    public @interface Greeting {

     

        public enum FontColor {

            RED, GREEN, BLUE

        };

     

        String name();

     

        String content();

     

        FontColor fontColor() default FontColor.BLUE;

    }

    正如上面代码所展示的,我们只允许Greeting注释标注在普通方法和构造函数上,使用在包申明、类名等时,会提示错误信息。

     

    注释保持性策略

        Java编译器编译时,它会识别在源代码里添加的注释是否还会保留,这就是RetentionPolicy。下面是Java定义的RetentionPolicy枚举:

    编译器的处理有三种策略:

    Ø         将注释保留在编译后的类文件中,并在第一次加载类时读取它

    Ø         将注释保留在编译后的类文件中,但是在运行时忽略它

    Ø         按照规定使用注释,但是并不将它保留到编译后的类文件中

    package java.lang.annotation;

     

    public enum RetentionPolicy {

      SOURCE,       // Annotation is discarded by the compiler

      CLASS,        // Annotation is stored in the class file, but ignored by the VM

      RUNTIME       // Annotation is stored in the class file and read by the VM

    }

        RetentionPolicy的使用方法与ElementType类似,简单代码示例如下:

    package com.gelc.annotation.demo.customize;

     

    import java.lang.annotation.ElementType;

    import java.lang.annotation.Retention;

    import java.lang.annotation.RetentionPolicy;

    import java.lang.annotation.Target;

     

    @Retention(RetentionPolicy.RUNTIME)

    @Target( { ElementType.METHOD, ElementType.CONSTRUCTOR })

    public @interface Greeting {

     

        public enum FontColor {

            RED, GREEN, BLUE

        };

     

        String name();

     

        String content();

     

        FontColor fontColor() default FontColor.BLUE;

    }

     

    文档化功能

        Java提供的Documented元注释跟Javadoc的作用是差不多的,其实它存在的好处是开发人员可以定制Javadoc不支持的文档属性,并在开发中应用。它的使用跟前两个也是一样的,简单代码示例如下:

    package com.gelc.annotation.demo.customize;

     

    import java.lang.annotation.Documented;

    import java.lang.annotation.ElementType;

    import java.lang.annotation.Retention;

    import java.lang.annotation.RetentionPolicy;

    import java.lang.annotation.Target;

     

    @Documented

    @Retention(RetentionPolicy.RUNTIME)

    @Target( { ElementType.METHOD, ElementType.CONSTRUCTOR })

    public @interface Greeting {

     

        public enum FontColor {

            RED, GREEN, BLUE

        };

     

        String name();

     

        String content();

     

        FontColor fontColor() default FontColor.BLUE;

    }

     

    值得大家注意的是,如果你要使用@Documented元注释,你就得为该注释设置RetentionPolicy.RUNTIME保持性策略。为什么这样做,应该比较容易理解,这里就不提了。

     

    标注继承

    继承应该是Java提供的最复杂的一个元注释了,它的作用是控制注释是否会影响到子类,简单代码示例如下:

    package com.gelc.annotation.demo.customize;

     

    import java.lang.annotation.Documented;

    import java.lang.annotation.ElementType;

    import java.lang.annotation.Inherited;

    import java.lang.annotation.Retention;

    import java.lang.annotation.RetentionPolicy;

    import java.lang.annotation.Target;

     

    @Inherited

    @Documented

    @Retention(RetentionPolicy.RUNTIME)

    @Target( { ElementType.METHOD, ElementType.CONSTRUCTOR })

    public @interface Greeting {

     

        public enum FontColor {

            RED, GREEN, BLUE

        };

     

        String name();

     

        String content();

     

        FontColor fontColor() default FontColor.BLUE;

    }

     

    概要

        你一定已经感受到元注释的好处和作用了吧,它可以让我们定制的注释功能更明确,约束性更强等好处;在下一篇文章中,我们将学习通过反射来获得这些注释信息。

     

    发表于 @ 2007年06月11日 10:56:00|评论(loading...)|编辑

    新一篇: Java Annotation(4) | 旧一篇: Java Annotation(2)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © numenzq