Tiger系列四:Annotation第二部分:定制Annotation

博客主要介绍Java中自定义Annotation类型,包括定义、添加成员、设置缺省值等操作。还阐述了元Annotation,如@Target指定应用元素,@Retention与编译器处理方式有关,@Documented使Javadoc包含Annotation,@Inherited可将Annotation信息带到子类。

1、自定义Annotation类型

1)定义Annotation类型

l         使用@interface声明Annotation类型

public @interface InProgress {
    
 
    
}
    

l         使用Annotation类型

public class TestAnnotation {
    
       @InProcess
    
       public void test() {
    
         
    
       }
    
}
    

l         如果Annotation类型和使用它的类不在相同的包中,可以import Annotation类型,以便直接使用 @InProgress

2)添加成员

l         Annotation类型可以有成员变量,以提供有用的信息

l         定义数据成员不需要定义gettersetter方法,只需要定义一个以成员名称命名的方法,并指定返回类型为需要的数据类型

l         简单的例子:

public @interface TODO {
    
       String value();
    
}
    

l         使用带成员的Annotation类型:

public class TestAnnotation {
    
       @InProcess
    
       @TODO("Need to finish this method later")
    
       public void test() {
    
         
    
       }
    
}
    

3)设置缺省值

l         要为Annotation类型的成员设置缺省值,需要在声明成员时使用default关键字:

public @interface GroupTODO {
    
       public enum Severity {
    
              CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION
    
       };
    
 
    
       Severity severity() default Severity.IMPORTANT;
    
       String item();
    
       String assignedTo();
    
       String dateAssigned();
    
}
    

l         当然,缺省值的类型必须与成员变量声明的类型完全相同

l         下面是使用缺省值的例子:

public class TestAnnotation {
    
       @InProcess
    
       @GroupTODO(
    
              item="Need to finish this method later",
    
              assignedTo="nelson_tu",
    
              dateAssigned="2005/02/05"
    
       )
    
       public void test() {
    
         
    
       }
    
}
    

l         下面是改写缺省值的例子:

public class TestAnnotation {
    
       @InProcess
    
       //@TODO("Need to finish this method later")
    
       @GroupTODO(
    
              severity=GroupTODO.Severity.DOCUMENTATION,
    
              item="Need to finish this method later",
    
              assignedTo="nelson_tu",
    
              dateAssigned="2005/02/05"
    
       )
    
       public void test() {
    
         
    
       }
    
}
    

 

2、元Annotation

l         Annotation就是AnnotationAnnotationJDK5提供了4种预定义的元Annotation

1@Target

l         @Target指定Annotation类型可以应用的程序元素,以便在其它程序元素中误用Annotation类型

l         程序元素的类型由java.lang.annotation.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
    
}
    

l         下面是使用@Target的例子:

@Target({ElementType.TYPE,
    
    ElementType.METHOD,
    
    ElementType.CONSTRUCTOR,
    
    ElementType.ANNOTATION_TYPE})
    
public @interface TODO {
    
       String value();
    
}
    

2@Retention

l         @Retention Java 编译器处理Annotation类型的方式有关

l         这些方式由java.lang.annotation.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
    
}
    

l         使用@Retention的例子参看后面的@Documented

3@Documented

l         @Documented指明需要在Javadoc中包含Annotation(缺省是不包含的)

l         下面是一个使用@Documented的例子:

@Documented
    
@Retention(RetentionPolicy.RUNTIME)
    
public @interface InProcess {
    
 
    
}
    

l         使用@Documented的一个技巧就是指定保持性策略为RetentionPolicy.RUNTIME:这样,Annotation就会保留在编译后的类文件中并且由虚拟机加载,然后Javadoc就可以抽取出Annotation,添加到类的HTML文档中

4@Inherited

l         @Inherited最复杂、使用最少、也最容易造成混淆的一个

l         假设使用@InProgress 标记一个正在开发的类,只要正确应用@DocumentedAnnotation信息就会出现在Javadoc中;现在要编写一个新类,扩展那个正在开发的类,那么使用子类,或者查看它的文档,根本没法表明还有什么地方没有完成;而本来是希望@InProgress Annotation信息会被带到子类中,这就需要使用@Inherited

l         下面是这样的例子:

@Documented
    
@Inherited
    
@Retention(RetentionPolicy.RUNTIME)
    
public @interface InProcess {
    
 
    
}
    

 

### osgEarth::Annotation 的概述 `osgEarth::Annotation` 是 osgEarth 库中的一个模块,用于在地球表面添加各种标注元素,如点、线、多边形、图片等。它提供了一系列的类和工具,方便用户在地理空间中创建和管理标注对象。 ### 使用方法及在 osgEarth 表面添加边长 500 米图片的应用 #### 包含必要的头文件 ```cpp #include <osg/Group> #include <osgViewer/Viewer> #include <osgEarth/MapNode> #include <osgEarth/GeoData> #include <osgEarth/Annotation> #include <osgEarthUtil/EarthManipulator> ``` #### 创建 Viewer 和 MapNode ```cpp osgViewer::Viewer viewer; osgEarth::Map* map = new osgEarth::Map(); osgEarth::MapNode* mapNode = new osgEarth::MapNode(map); osg::Group* root = new osg::Group(); root->addChild(mapNode); viewer.setSceneData(root); viewer.setCameraManipulator(new osgEarth::Util::EarthManipulator()); ``` #### 定义图片的位置和大小 ```cpp osgEarth::GeoPoint center(osgEarth::SpatialReference::get("wgs84"), 0.0, 0.0, 0.0); double sideLength = 500.0; ``` #### 加载图片 ```cpp osg::ref_ptr<osg::Image> image = osgDB::readRefFile<osg::Image>("your_image.png"); if (!image.valid()) { return -1; } ``` #### 创建 ImageOverlay 并设置属性 ```cpp osgEarth::Annotation::ImageOverlay* overlay = new osgEarth::Annotation::ImageOverlay(mapNode->getMapSRS()); overlay->setImage(image.get()); overlay->setSize(sideLength, sideLength); overlay->setPosition(center); ``` #### 将 ImageOverlay 添加到 MapNode ```cpp mapNode->addChild(overlay); ``` #### 运行 Viewer ```cpp return viewer.run(); ``` ### 代码解释 - `osgEarth::Annotation::ImageOverlay` 类用于在地球表面添加图片标注。 - `setImage` 方法用于设置要显示的图片。 - `setSize` 方法用于设置图片的大小。 - `setPosition` 方法用于设置图片的位置。 ### 其他标注类型 除了 `ImageOverlay`,`osgEarth::Annotation` 还提供了其他类型的标注,如 `PointNode`、`LineStringNode`、`PolygonNode` 等。以下是一个添加点标注的示例: ```cpp osgEarth::Annotation::PointNode* point = new osgEarth::Annotation::PointNode( mapNode->getMapSRS(), center, osgEarth::Symbology::Style() ); mapNode->addChild(point); ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值