java 注解annotation的使用,以及反射如何获取注解

 一、注解基本知识

分类: java

1、元注解

元注解是指注解的注解。包括  @Retention @Target @Document @Inherited 四种。

1. Annotation 型定义为 @interface, 所有的 Annotation 会自动继承 java.lang.Annotation 这一接口, 并且不能再去继承别的类或是接口.

2. 参数成员只能用 public 或默认 (default) 这两个访问权修饰

3. 参数成员只能用基本类型 byte,short,char,int,long,float,double,boolean 八种基本数据类型和 String、Enum、Class、annotations 等数据类型, 以及这一些类型的数组.

4. 要获取类方法和字段的注解信息,必须通过 Java 的反射技术来获取 Annotation 对象, 因为你除此之外没有别的获取注解对象的方法

5. 注解也可以没有定义成员, 不过这样注解就没啥用了

自定义注解类时, 可以指定目标 (类、方法、字段, 构造函数等) , 注解的生命周期 (运行时,class 文件或者源码中有效), 是否将注解包含在 javadoc 中及是否允许子类继承父类中的注解, 具体如下:

1. @Target 表示该注解目标, 可能的 ElemenetType 参数包括:

ElemenetType.CONSTRUCTOR 构造器声明
ElemenetType.FIELD 域声明 (包括 enum 实例) 
ElemenetType.LOCAL_VARIABLE 局部变量声明 
ElemenetType.METHOD 方法声明 
ElemenetType.PACKAGE 包声明 
ElemenetType.PARAMETER 参数声明 
ElemenetType.TYPE 类,接口 (包括注解类型) 或 enum 声明

2. @Retention 表示该注解的生命周期, 可选的 RetentionPolicy 参数包括

RetentionPolicy.SOURCE 注解将被编译器丢弃 
RetentionPolicy.CLASS 注解在 class 文件中可用,但会被 VM 丢弃 
RetentionPolicy.RUNTIME VM 将在运行期也保留注释,因此可以通过反射机制读取注解的信息

3. @Documented 指示将此注解包含在 javadoc 中

4.  @Inherited 指示允许子类继承父类中的注解

二、在 java 中如何使用 

   2.1、定义注解

package com.test.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class MyAnnotation {

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 注解类
 * </span><span style="color: rgba(128, 128, 128, 1)">@author</span><span style="color: rgba(0, 128, 0, 1)"> T4980D
 *
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.TYPE)  
</span><span style="color: rgba(0, 0, 255, 1)">public</span> @<span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> MyClassAnnotation {  
    String uri();  
    String desc();  
}  

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 * 构造方法注解 
 * </span><span style="color: rgba(128, 128, 128, 1)">@author</span><span style="color: rgba(0, 128, 0, 1)"> T4980D 
 * 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">  
@Retention(RetentionPolicy.RUNTIME)     
@Target(ElementType.CONSTRUCTOR)   
</span><span style="color: rgba(0, 0, 255, 1)">public</span> @<span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> MyConstructorAnnotation {  
  
    String uri();  
    String desc();  
}  

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 * 我的方法注解 
 * </span><span style="color: rgba(128, 128, 128, 1)">@author</span><span style="color: rgba(0, 128, 0, 1)"> Owner 
 * 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">  
@Retention(RetentionPolicy.RUNTIME)     
@Target(ElementType.METHOD)  
</span><span style="color: rgba(0, 0, 255, 1)">public</span> @<span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> MyMethodAnnotation {  
  
    String uri();  
    String desc();  
}  

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 
 * 字段注解定义 
 * </span><span style="color: rgba(128, 128, 128, 1)">@author</span><span style="color: rgba(0, 128, 0, 1)"> Owner 
 * 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">  
@Retention(RetentionPolicy.RUNTIME)     
@Target(ElementType.FIELD)   
</span><span style="color: rgba(0, 0, 255, 1)">public</span> @<span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> MyFieldAnnotation {  
  
    String uri();  
    String desc();  
}  
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 
 * 可以同时应用到类上和方法上
 * </span><span style="color: rgba(128, 128, 128, 1)">@author</span><span style="color: rgba(0, 128, 0, 1)"> T4980D
 *
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> @<span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> Yts {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义枚举</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">enum</span><span style="color: rgba(0, 0, 0, 1)"> YtsType {
        util, entity, service, model
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置默认值</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> YtsType classType() <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> YtsType.util;
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数组</span>
    <span style="color: rgba(0, 0, 255, 1)">int</span>[] arr() <span style="color: rgba(0, 0, 255, 1)">default</span> {3, 7, 5<span style="color: rgba(0, 0, 0, 1)">};

    String color() </span><span style="color: rgba(0, 0, 255, 1)">default</span> "blue"<span style="color: rgba(0, 0, 0, 1)">;
}

}

  2.2、基本测试注解

package com.test.annotation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.test.annotation.MyAnnotation.MyClassAnnotation;
import com.test.annotation.MyAnnotation.MyConstructorAnnotation;
import com.test.annotation.MyAnnotation.MyFieldAnnotation;
import com.test.annotation.MyAnnotation.MyMethodAnnotation;
import com.test.annotation.MyAnnotation.Yts;
import com.test.annotation.MyAnnotation.Yts.YtsType;

@MyClassAnnotation(desc = "The class", uri = "com.test.annotation.Test")
@Yts(classType
=YtsType.util)
public class TestAnnotation {
@MyFieldAnnotation(desc
= "The class field", uri = "com.test.annotation.Test#id")
private String id;

@MyConstructorAnnotation(desc </span>= "The class constructor", uri = "com.test.annotation.Test#MySample"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TestAnnotation() {
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getId() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id;
}

@MyMethodAnnotation(desc </span>= "The class method", uri = "com.test.annotation.Test#setId"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setId(String id) {
    System.out.println(</span>" method info: "+<span style="color: rgba(0, 0, 0, 1)">id);
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id;
}

@MyMethodAnnotation(desc </span>= "The class method sayHello", uri = "com.test.annotation.Test#sayHello"<span style="color: rgba(0, 0, 0, 1)">)
@Yts  
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> sayHello(String name){  
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(name == <span style="color: rgba(0, 0, 255, 1)">null</span> || name.equals(""<span style="color: rgba(0, 0, 0, 1)">)){  
        System.out.println(</span>"hello world!"<span style="color: rgba(0, 0, 0, 1)">);  
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{  
        System.out.println(name </span>+ "\t:say hello world!"<span style="color: rgba(0, 0, 0, 1)">);  
    }  
}  
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {

    Class</span>&lt;TestAnnotation&gt; clazz = TestAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 得到类注解</span>
    MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(myClassAnnotation.desc() </span>+ " "+<span style="color: rgba(0, 0, 0, 1)"> myClassAnnotation.uri());

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 得到构造方法注解</span>
    Constructor&lt;TestAnnotation&gt; cons = clazz.getConstructor(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Class[]{});
    MyConstructorAnnotation myConstructorAnnotation </span>= cons.getAnnotation(MyConstructorAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(myConstructorAnnotation.desc() </span>+ " "+<span style="color: rgba(0, 0, 0, 1)"> myConstructorAnnotation.uri());

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取方法注解</span>
    Method method = clazz.getMethod("setId", <span style="color: rgba(0, 0, 255, 1)">new</span> Class[]{<span style="color: rgba(0, 0, 255, 1)">int</span>.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">});
    MyMethodAnnotation myMethodAnnotation </span>= method.getAnnotation(MyMethodAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(myMethodAnnotation.desc() </span>+ " "+<span style="color: rgba(0, 0, 0, 1)"> myMethodAnnotation.uri());
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取字段注解</span>
    Field field = clazz.getDeclaredField("id"<span style="color: rgba(0, 0, 0, 1)">);
    MyFieldAnnotation myFieldAnnotation </span>= field.getAnnotation(MyFieldAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(myFieldAnnotation.desc() </span>+ " "+<span style="color: rgba(0, 0, 0, 1)"> myFieldAnnotation.uri());
}

}

  2.3、通过反射解析

package com.test.annotation;

import java.lang.reflect.Method;
import java.util.Arrays;

import com.test.annotation.MyAnnotation.MyClassAnnotation;
import com.test.annotation.MyAnnotation.MyMethodAnnotation;
import com.test.annotation.MyAnnotation.Yts;
import com.test.annotation.MyAnnotation.Yts.YtsType;

public class ParseAnnotation {

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 解析方法注解
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> &lt;T&gt;
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> clazz
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> &lt;T&gt; <span style="color: rgba(0, 0, 255, 1)">void</span> parseMethod(Class&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> clazz) {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        T obj </span>=<span style="color: rgba(0, 0, 0, 1)"> clazz.newInstance();
        </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Method method : clazz.getDeclaredMethods()) {
            MyMethodAnnotation methodAnnotation </span>= method.getAnnotation(MyMethodAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (methodAnnotation!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过反射调用带有此注解的方法</span>

method.invoke(obj, methodAnnotation.uri());
}
Yts yts
= (Yts) method.getAnnotation(Yts.class);
if (yts != null) {
if (YtsType.util.equals(yts.classType())) {
System.out.println(
"this is a util method");
}
else {
System.out.println(
"this is a other method");
}
System.out.println(Arrays.toString(yts.arr()));
//打印数组
System.out.println(yts.color()); //输出颜色
}
System.out.println(
"\t\t-----------------------");
}
}
catch (Exception e) {
e.printStackTrace();
}
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 解析类注解
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> &lt;T&gt;
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> clazz
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> &lt;T&gt; <span style="color: rgba(0, 0, 255, 1)">void</span> parseType(Class&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> clazz) {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        Yts yts </span>= (Yts) clazz.getAnnotation(Yts.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (yts != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (YtsType.util.equals(yts.classType())) {
                System.out.println(</span>"this is a util class"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                System.out.println(</span>"this is a other class"<span style="color: rgba(0, 0, 0, 1)">);
            }
        }
        MyClassAnnotation classAnnotation </span>= (MyClassAnnotation) clazz.getAnnotation(MyClassAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (classAnnotation != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            System.err.println(</span>" class info: "+<span style="color: rgba(0, 0, 0, 1)">classAnnotation.uri());
        }
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
        e.printStackTrace();
    }
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    parseMethod(TestAnnotation.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    parseType(TestAnnotation.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
}

}

 三、注解应用案例

    3.1、关于细粒度权限拦截的问题,在 Struts2 中可以根据登录用户所具有的的权限进行任一个 action 方法的拦截,可以定义一个自定义方法注解,例如

@Retention(RetentionPolicy.RUNTIME)//代表 Permission 注解保留在的阶段  
@Target(ElementType.METHOD)//标注在方法上面  
public @interface Permission {  
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 模块 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">  
String module();  
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> 权限值 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">  
String privilege();  

}

  3、2 比如有一个部门 action,Department.action, 有一个方法 public String departmentlistUI(){}可以这样定义方法

@Permission(module="department",privilege="view")  
public String departmentlistUI(){  

}

  3.3、然后自定定义一个权限拦截器 PrivilegeInterceptor.java 并在 struts.xml 中注册,在实现 interceptor 接口后,实现方法 public String intercept(ActionInvocation invocation) throws Exception {},在这里调用任一个 action 方法都会经过该拦截方法,通过 invocation 可以获取当前调用的 action 的名字,以及调用的 action 的哪个方法,通过这段代码可以获取 action 名字和方法名。

String  actionName=invocation.getProxy().getActionName();  
String  methodName=invocation.getProxy().getMethod();  

System.out.println("拦截到:action 的名字:"+actionName+"方法名:"+methodName);

  4、然后通过反射技术,获取该方法上的自定义权限注解,获取当前登录的用户(从 session 中),遍历当前用户的所拥有的权限组,并且遍历任一个权限组下的所有的权限,看是否包括该方法上注解所需的权限。这样就可以完成细粒度的 action 方法权限拦截了。

private boolean validate(ActionInvocation invocation) throws SecurityException, NoSuchMethodException {  
    String  methodName</span>=<span style="color: rgba(0, 0, 0, 1)">invocation.getProxy().getMethod();  
      
    Method currentMethod </span>=<span style="color: rgba(0, 0, 0, 1)"> invocation.getAction().getClass().getMethod(methodName);  
      
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(currentMethod != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp; currentMethod.isAnnotationPresent(Permission.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)){  
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到方法上的注解  </span>
        Permission permission = currentMethod.getAnnotation(Permission.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);  
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该方法上的所需要的权限  </span>
        SystemPrivilege methodPrivilege = <span style="color: rgba(0, 0, 255, 1)">new</span> SystemPrivilege(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SystemPrivilegePK(permission.module(), permission.privilege()));  
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到当前登录的用户  </span>
        Employee e = (Employee) ActionContext.getContext().getSession().get("loginUser"<span style="color: rgba(0, 0, 0, 1)">);  
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">遍历当前用户下的所有的权限组  </span>
        <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(PrivilegeGroup group : e.getGroups()){  
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果该权限组下包含,要访问该方法所需要的权限,就放行  </span>
            <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(group.getPrivileges().contains(methodPrivilege)){  
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;  
            }  
              
        }  
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">说明遍历的该用户所有的权限组,没有发现该权限,说明没有该权限  </span>
        <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;  
           
    }  
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">没有标注注解,表示谁都可以调用该方法  </span>
    <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;  
}  </span></pre>