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

信阳网站建设招聘东莞vi设计

信阳网站建设招聘,东莞vi设计,哈尔滨做网站哪好,网站发多少篇文章开始做外链Java基础之注解 一、Java注解1.1、内置注解1.2、元注解1.3、自定义注解 一、Java注解 注解#xff08;Annotation#xff09;是一种为程序代码提供元数据#xff08;metadata#xff09;的方式。注解提供了关于程序代码的额外信息#xff0c;这些信息可以在运行时或编译时… Java基础之注解 一、Java注解1.1、内置注解1.2、元注解1.3、自定义注解 一、Java注解 注解Annotation是一种为程序代码提供元数据metadata的方式。注解提供了关于程序代码的额外信息这些信息可以在运行时或编译时被读取并处理。Java 的注解以 符号开头例如 Override、Deprecated 等。 1.1、内置注解 Override 表示该方法覆盖了父类的方法。通常用于帮助编译器检查是否正确地覆盖了父类的方法。 Deprecated 表示该方法或类已经过时不推荐使用。编译器在使用过时的方法或类时会发出警告。 SuppressWarnings 抑制编译器警告。可以用于特定的方法或整个类。 class Parent {public void printMessage() {System.out.println(Parent class);} }class Child extends Parent {Overridepublic void printMessage() {System.out.println(Child class);} }class DeprecatedExample {Deprecatedpublic void oldMethod() {System.out.println(This method is deprecated.);} }class SuppressWarningExample {SuppressWarnings(unchecked)public void uncheckedOperation() {// 一些操作} }public class AnnotationExample {public static void main(String[] args) {// 使用 Override 注解Parent parent new Child();parent.printMessage(); // 输出 Child class// 使用 Deprecated 注解DeprecatedExample example new DeprecatedExample();example.oldMethod(); // 输出 This method is deprecated.// 使用 SuppressWarnings 注解SuppressWarningExample suppressWarningExample new SuppressWarningExample();suppressWarningExample.uncheckedOperation(); // 不会产生未经检查的警告} }1.2、元注解 元注解是应用于其他注解的注解用于控制注解的行为。 Retention 指定注解的保留策略即注解在何时可用。有 RetentionPolicy.SOURCE、RetentionPolicy.CLASS 和 RetentionPolicy.RUNTIME 三种策略。Target 指定注解可以应用于的元素类型如类、方法、字段等。Documented 表示注解将包含在 JavaDoc 文档中。 import java.lang.annotation.*;Retention(RetentionPolicy.RUNTIME) Target({ElementType.METHOD, ElementType.TYPE}) Documented Inherited public interface MyCustomAnnotation {String value() default default value;int count() default 0; }MyCustomAnnotation(value Custom Value, count 42) class AnnotatedClass {MyCustomAnnotation(value Custom Method)public void annotatedMethod() {// 方法实现} }public class AnnotationIntegrationExample {public static void main(String[] args) {// 获取类级别的注解信息MyCustomAnnotation classAnnotation AnnotatedClass.class.getAnnotation(MyCustomAnnotation.class);System.out.println(Class Annotation Value: classAnnotation.value());System.out.println(Class Annotation Count: classAnnotation.count());// 获取方法级别的注解信息try {AnnotatedClass annotatedInstance new AnnotatedClass();MyCustomAnnotation methodAnnotation annotatedInstance.getClass().getMethod(annotatedMethod).getAnnotation(MyCustomAnnotation.class);System.out.println(Method Annotation Value: methodAnnotation.value());System.out.println(Method Annotation Count: methodAnnotation.count());} catch (NoSuchMethodException e) {e.printStackTrace();}} }1.3、自定义注解 自定义注解的定义方式类似于接口但使用 interface 关键字。 interface用来声明一个注解格式public interface注解名{定义内容}其中的每一个方法实际上是声明了一个配置参数方法的名称就是参数的名称返回值类型就是参数的类型返回值只能是基本类型 Class, String,enum可以通过 defau来声明参数的默认值如果只有一个参数成员一般参数名为vaue注解元素必须要有值我们定义注解元素时经常使用空字符串0作为默认值 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.TYPE, ElementType.METHOD}) public interface MyCustomAnnotation {String value() default default value;int count() default 0; }// 使用自定义注解 MyCustomAnnotation(value Custom Class, count 10) public class AnnotatedClass {MyCustomAnnotation(value Custom Method, count 5)public void annotatedMethod() {// 方法实现} }public class CustomAnnotationExample {public static void main(String[] args) {// 获取类级别的注解信息MyCustomAnnotation classAnnotation AnnotatedClass.class.getAnnotation(MyCustomAnnotation.class);System.out.println(Class Annotation Value: classAnnotation.value());System.out.println(Class Annotation Count: classAnnotation.count());// 获取方法级别的注解信息try {AnnotatedClass annotatedInstance new AnnotatedClass();MyCustomAnnotation methodAnnotation annotatedInstance.getClass().getMethod(annotatedMethod).getAnnotation(MyCustomAnnotation.class);System.out.println(Method Annotation Value: methodAnnotation.value());System.out.println(Method Annotation Count: methodAnnotation.count());} catch (NoSuchMethodException e) {e.printStackTrace();}} }
http://www.huolong8.cn/news/76555/

相关文章:

  • 毕节市交通建设集团网站河池市住房城乡建设网站
  • 内蒙古网站建设手工制作衣服童装环保
  • 通过网站做外贸wordpress主题首页文件夹
  • 高端网站设计制作方法上海企业排行榜
  • 网站开发 法律网上书城网站开发背景
  • 广州网站建设正规公司做psd模板下载网站
  • 网站运行及维护wordpress权限设置方法
  • 17zwd一起做业网站免费网站推广网站在线
  • 政务公开网站开发江门专业网站建设公司
  • 做电商哪几个设计网站比较好境内境外网站区别
  • 网站维护模式企业建设网站没有服务器
  • 网站设计跟网页制作河北集团网站建设
  • 昭通网站建设兼职网站开发的就业方向
  • 贵州省教育厅办公室关于开展2014年学校门户网站建设评估的通知银川企业网站设计制作
  • 网站开发校园经历范文购物网站搜索功能怎么做
  • seo蒙牛伊利企业网站专业性诊断网络系统搭建
  • 传诚信网站建设diy网站开发公司
  • 昆明网站推广优化公司免费音效素材网站
  • 购物网站开发设计类图新手建什么网站赚钱
  • 网站有些什么内容做网站多少钱 优帮云
  • me域名公司网站深圳宝安区有哪些街道
  • 建设网站怎么判断是电脑还是手机阿里云服务器免费一年
  • 做网站如何月入10万建设企业网站需要了解什么
  • 小网站的制作网站首页设计图
  • 旅游精品网站建设建筑公司logo设计大全
  • 汽车配件生产企业网站模板南通哪里做网站
  • 网站开发类专业服务文案开源 wordpress 主题
  • 单位网站和新媒体建设制度中小企业网站建设济南兴田德润o厉害吗
  • 自己电脑建设网站苏州网站运营公司
  • 如何制作自己网站淘宝的网站建设费用