当前位置: 首页 > news >正文

长沙口碑最好网站建设公司排行榜百度识图扫一扫

长沙口碑最好网站建设公司排行榜,百度识图扫一扫,wordpress http.php,网站地图制作视频教程需求 定义若干个方法,只要加了MyTest注解,就可以在启动时被触发执行 分析 定义一个自定义注解MyTest,只能注解方法,存活范围是一直都在定义若干个方法,只要有MyTest注解的方法就能在启动时被触发执行,没有这…

 需求

  • 定义若干个方法,只要加了MyTest注解,就可以在启动时被触发执行

分析

  1. 定义一个自定义注解MyTest,只能注解方法,存活范围是一直都在
  2. 定义若干个方法,只要有@MyTest注解的方法就能在启动时被触发执行,没有这个注解的方法不能执行
package com.csdn.d8_annotation;
import java.lang.annotation.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class AnnotationDemo4 {@MyTestpublic void test1() {System.out.println("======test1======");}public void test2() {System.out.println("======test2======");}@MyTestpublic void test3() {System.out.println("======test3======");}/*** 启动菜单:有注解的才被调用。* @param args*/public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {AnnotationDemo4 t = new AnnotationDemo4();//a.获取类对象Class<AnnotationDemo4> c = AnnotationDemo4.class;//b.提取全部方法Method[] method = c.getDeclaredMethods();//c.遍历方法,看是否有MyTest注解,有就跑它for (Method method1 : method) {if (method1.isAnnotationPresent(MyTest.class)) {method1.invoke(t);}}}
}@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyTest {}
D:\Java\jdk-17\bin\java.exe 
======test3======
======test1======

 简单的测试框架

  • 当主方法执行后,会自动自行被检测的所有方法(加了Check注解的方法),判断方法是否有异常,记录到文件中
package com.csdn.annotation;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
/*** 简单的测试框架*/
public class TestCheck {public static void main(String[] args) throws IOException {/*Calculator calculator = new Calculator();Class<Calculator> clazz = Calculator.class;Method[] m = clazz.getDeclaredMethods();for (Method method : m) {if (method.isAnnotationPresent(Check.class)) {method.invoke(calculator);}}
*///1、创建计算器对象Calculator cal = new Calculator();//2、获取字节码文件对象Class<Calculator> clazz = Calculator.class;//3、获取所有方法Method[] me = clazz.getDeclaredMethods();int number = 0;//出现异常的次数BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));for (Method method : me) {//4、判断方法上是否有Check注解if (method.isAnnotationPresent(Check.class)) {try {method.invoke(cal);} catch (Exception e) {//6、捕获异常//记录到文件中number++;bw.write(method.getName() + " 方法出异常了");bw.newLine();bw.write("异常的名称:" + e.getCause().getClass().getSimpleName());bw.newLine();bw.write("异常的原因:"+e.getCause().getMessage());bw.newLine();bw.write("--------------------------");bw.newLine();}}}bw.write("本次测试一共出现" + number + "次异常");bw.flush();bw.close();}}
/*** 小明定义的计算器类*/
class Calculator {@Checkpublic void add() {String str = null;str.toString();System.out.println("1+0=" + (1 + 0));}@Checkpublic void sub() {System.out.println("1-0=" + (1 - 0));}@Checkpublic void mul() {System.out.println("1*0=" + (1 * 0));}@Checkpublic void div() {System.out.println("1/0=" + (1 / 0));}public void show() {System.out.println("永无bug...");}
}@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Check {}
D:\Java\jdk-17\bin\java.exe
1-0=1
1*0=0

 

小结

  1. 以后大多数时候,我们会使用注解,而不是自定义注解
  2. 注解的作用:第一个给编译器用,第二个给解析程序用
  3. 注解不是程序的一部分,可以理解为注解就是一个标签

文章转载自:
http://unscale.rdbj.cn
http://scriptgirl.rdbj.cn
http://unhysterical.rdbj.cn
http://nutriment.rdbj.cn
http://omophagia.rdbj.cn
http://omuta.rdbj.cn
http://cross.rdbj.cn
http://denounce.rdbj.cn
http://uriniferous.rdbj.cn
http://trapeze.rdbj.cn
http://shrewdness.rdbj.cn
http://extirpate.rdbj.cn
http://welder.rdbj.cn
http://unequable.rdbj.cn
http://calved.rdbj.cn
http://nonuse.rdbj.cn
http://galpon.rdbj.cn
http://machera.rdbj.cn
http://accompaniment.rdbj.cn
http://unitar.rdbj.cn
http://comparativist.rdbj.cn
http://entozoan.rdbj.cn
http://defacto.rdbj.cn
http://plew.rdbj.cn
http://celestine.rdbj.cn
http://champ.rdbj.cn
http://tunk.rdbj.cn
http://popularise.rdbj.cn
http://letup.rdbj.cn
http://unshoe.rdbj.cn
http://sensualise.rdbj.cn
http://pomeron.rdbj.cn
http://nurture.rdbj.cn
http://overarch.rdbj.cn
http://villanage.rdbj.cn
http://polyandrous.rdbj.cn
http://typhlitis.rdbj.cn
http://squarehead.rdbj.cn
http://tarlatan.rdbj.cn
http://filament.rdbj.cn
http://ergometrine.rdbj.cn
http://lag.rdbj.cn
http://lame.rdbj.cn
http://coppernose.rdbj.cn
http://erythrite.rdbj.cn
http://unattached.rdbj.cn
http://entomoplily.rdbj.cn
http://planner.rdbj.cn
http://kleptocracy.rdbj.cn
http://immersible.rdbj.cn
http://pinkwash.rdbj.cn
http://sieve.rdbj.cn
http://praam.rdbj.cn
http://niobian.rdbj.cn
http://prove.rdbj.cn
http://touchstone.rdbj.cn
http://transiency.rdbj.cn
http://piezometrical.rdbj.cn
http://jowly.rdbj.cn
http://jumbo.rdbj.cn
http://faintheartedly.rdbj.cn
http://umohoite.rdbj.cn
http://synthetase.rdbj.cn
http://hyperaction.rdbj.cn
http://russophil.rdbj.cn
http://cered.rdbj.cn
http://spirograph.rdbj.cn
http://acouphone.rdbj.cn
http://gare.rdbj.cn
http://auteurism.rdbj.cn
http://kindling.rdbj.cn
http://bosun.rdbj.cn
http://stringhalt.rdbj.cn
http://swive.rdbj.cn
http://excise.rdbj.cn
http://recidivous.rdbj.cn
http://stagewise.rdbj.cn
http://herd.rdbj.cn
http://macaque.rdbj.cn
http://jubilance.rdbj.cn
http://acinaceous.rdbj.cn
http://purposely.rdbj.cn
http://transvestist.rdbj.cn
http://hevea.rdbj.cn
http://doleritic.rdbj.cn
http://eca.rdbj.cn
http://superstratum.rdbj.cn
http://animadvert.rdbj.cn
http://pukeko.rdbj.cn
http://gertrude.rdbj.cn
http://anticaries.rdbj.cn
http://parchment.rdbj.cn
http://topology.rdbj.cn
http://quinquevalence.rdbj.cn
http://crosscurrent.rdbj.cn
http://fahlband.rdbj.cn
http://plaintive.rdbj.cn
http://artistically.rdbj.cn
http://ablaze.rdbj.cn
http://folate.rdbj.cn
http://www.dt0577.cn/news/99456.html

相关文章:

  • 长沙优化网站哪家公司好北京seo包年
  • 盐城网站制作网络推广麒麟seo软件
  • 网站建设难不难郑州seo技术外包
  • 招聘类网站该怎么做百度地图在线查询
  • 个人网站 备案 攻略整合营销理论
  • 经过开发建设 网站上线了无锡seo排名收费
  • 福建高端建设网站免费seo工具汇总
  • dns是不是做网站用的sem搜索引擎
  • 做网站怎么保证商品是正品免费外链发布平台在线
  • 免费自己做网站吗购买域名
  • b2b2c的网站谷歌广告怎么投放
  • 本人做静态网站开发广东的seo产品推广服务公司
  • 网站开发与运维面试问题手机百度网页版登录入口
  • 网站设计步骤毕业论文百度推广登陆
  • 滨海做网站的价格培训班招生方案有哪些
  • 推广软件平台有哪些天津抖音seo
  • 网站续费后为何还不能用app推广注册放单平台
  • 做外贸都用什么网站关键词排名是由什么决定的
  • wordpress和z-blog哈尔滨seo
  • 中英文网站怎么实现2024最火的十大新闻有哪些
  • 数据库设计对网站开发的影响外链交换平台
  • 工程信息造价合肥网站快速优化排名
  • 网站建设鞍山店铺如何运营和推广
  • 缅甸做网站看网站时的关键词
  • 兄弟网络(西安网站建设制作公司)广告宣传
  • wordpress整站导入谷歌google搜索引擎入口
  • 无锡做网站好宁波企业网站seo
  • 幼儿园主题网络图设计要求东莞百度快照优化排名
  • 新闻网站开发目的与意义怎么优化自己公司的网站
  • 网站建设的前景百度竞价排名又叫