⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample34_11.java

📁 Java SE 6.0前32-34章示的示例代码,简单易学
💻 JAVA
字号:
package wyf.jc;
import java.lang.annotation.*;
import java.lang.reflect.*;
//声明使用目标为METHOD的标注性注解
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD)
@interface MyAnnotationForMethod {
}
//声明使用注解的类
class MyClass
{
	//声名使用了标注性注解的方法
	@MyAnnotationForMethod
	public void sayHello()
	{
		System.out.println("恭喜您成功地调用了sayHello方法!!!");
	}
	//声名没有使用的方法
	public void sayByebye()
	{
		System.out.println("恭喜您成功地调用了sayByebye方法!!!");
	}	
}
//主类
public class Sample34_11
{
     public static void main(String[] args) throws NoSuchMethodException
     {
          //获取使用了注解的类MyClass对应的Class对象
          Class ac=MyClass.class;
          //获取MyClass所有声明方法对应的Method数组
          Method[] ma=ac.getDeclaredMethods();
          //对数组进行遍历打印出方法使用标注性注解的情况
          System.out.println("方法名称\t\t使用标注性注解的情况");
          int size=ma.length;
          for(int i=0;i<size;i++)
          {
          	Method tempm=ma[i];
          	//打印方法名称
          	System.out.print(tempm.getName()+"\t\t\t");
          	//获取使用标注性注解的情况
          	boolean flag=tempm.isAnnotationPresent(MyAnnotationForMethod.class);
          	//打印使用标注性注解的情况
          	System.out.println((flag)?"已使用":"未使用");
          }
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -