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

阳江网站制作关键词优化上海

阳江网站制作,关键词优化上海,橙色的网站,网站icp备案查询截图注解 大多数时候,我们会使用注解,而不是自定义注解。注解给谁用?编译器 、给解析程序用注解不是程序的一部分,可以理解为注解就是一个标签 主要的作用有以下四方面: 生成文档,通过代码里标识的元数据生成…

注解

  • 大多数时候,我们会使用注解,而不是自定义注解。
  • 注解给谁用?编译器 、给解析程序用
  • 注解不是程序的一部分,可以理解为注解就是一个标签

主要的作用有以下四方面:

  • 生成文档,通过代码里标识的元数据生成 javadoc 文档。

  • 编译检查,通过代码里标识的元数据让编译器在编译期间进行检查验证。

  • 编译时动态处理,编译时通过代码里标识的元数据动态处理,例如动态生成代码。

  • 运行时动态处理,运行时通过代码里标识的元数据动态处理,例如使用反射注入实例。

注解的本质

package com.jj.anno;import java.lang.annotation.Retention;
import java.lang.annotation.Target;/*** 拥有宠物的信息* 宠物的名字* 宠物的年龄*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.TYPE)
public @interface Pet {String name();int age();
}

反编译后发现,注解本质上是一个 interface并且继承了 java.lang.annotation.Annotation这个接口.

➜  anno git:(master) ✗ javac Pet.java && javap Pet
警告: 二进制文件Pet包含com.jj.anno.Pet
Compiled from "Pet.java"
public interface com.jj.anno.Pet extends java.lang.annotation.Annotation {public abstract java.lang.String name();public abstract int age();
}

Java 注解(Annotation)是一种元数据形式,提供有关程序代码的额外信息,但这些信息不会影响代码的实际执行。注解可以用于类、方法、变量、参数等程序元素上,以便在编译时、类加载时或运行时进行处理。

元注解

元注解(Meta-Annotations)是用于注解其他注解的注解。元注解提供了对注解进行更精细的控制和定义。

@Retention

@Retention 用于指定注解的保留策略,即注解可以在何时可用。它可以被用于定义其他注解。

public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time.  This is the default* behavior.*/CLASS,/*** Annotations are to be recorded in the class file by the compiler and* retained by the VM at run time, so they may be read reflectively.** @see java.lang.reflect.AnnotatedElement*/RUNTIME
}

@Retention 元注解有一个 value 属性,用于指定保留策略。可选的保留策略包括:

  • RetentionPolicy.SOURCE:注解仅在源代码级别可见,在编译之后不会包含在编译后的字节码中。
  • RetentionPolicy.CLASS:注解在编译时保留,在编译后的字节码中可见,但在运行时不可通过 反射 获取注解信息(默认值)。
  • RetentionPolicy.RUNTIME:注解在运行时保留,可以通过反射获取注解信息,并根据注解的定义执行相应的代码,对运行时的代码有影响。

自定义注解的保留策略如果不指定,默认为 RetentionPolicy.CLASS

编译器分别使用了 RuntimeInvisibleAnnotationsRuntimeVisibleAnnotations 属性去记录了 RetentionPolicy.CLASS 注解的方法 和 RetentionPolicy.RUNTIME 方法的注解信息。

@Target

描述注解的作用位置

public enum ElementType {/** Class, interface (including annotation interface), enum, or record* declaration */TYPE,/** Field declaration (includes enum constants) */FIELD,/** Method declaration */METHOD,/** Formal parameter declaration */PARAMETER,/** Constructor declaration */CONSTRUCTOR,/** Local variable declaration */LOCAL_VARIABLE,/** Annotation interface declaration (Formerly known as an annotation type.) */ANNOTATION_TYPE,/** Package declaration */PACKAGE,/*** Type parameter declaration** @since 1.8*/TYPE_PARAMETER,/*** Use of a type** @since 1.8*/TYPE_USE,/*** Module declaration.** @since 9*/MODULE,/*** Record component** @jls 8.10.3 Record Members* @jls 9.7.4 Where Annotations May Appear** @since 16*/RECORD_COMPONENT;
}

@Documented

  • @Documented :用于指定被它注解的注解是否应该包含在自动生成的 API 文档(Javadoc)中。它可以被用于定义其他注解。
  • @Documented :只是一个标记,它本身并不会影响注解的使用和行为。只有在生成 API 文档时才会体现其作用。

通常情况下,如果开发者希望在 API 文档中包含某个注解的信息,就可以为该注解添加 @Documented 元注解。如果不需要在 API 文档中包含注解信息,可以不添加该注解。

@Inherited

@Inherited 用于指定被它注解的 Annotation 是否可以被子类继承。它可以被用于定义其他注解。

自定义注解

@interface 是 Java 中用于创建自定义注解的关键字。使用 @interface 可以定义一个新的注解类型,并在注解中声明自定义的元素。

public @interface 注解名 {// 注解元素声明
}

@interface 定义的花括号中,可以声明注解的元素。

注解元素的定义类似于方法的定义,包括元素的类型、名称和可选的默认值。

注解的属性

属性定义方式与接口中的方法声明方式一样,在注解声明了方法,就同时声明了同名属性变量.

属性的赋值

注解的属性就是接口中定义的方法,定义了属性,就要给属性赋值.

  1. 如果定义属性时,使用 default关键字给属性默认初始化值,则使用注解时,可以不进行属性的赋值.
  2. 如果注解只声明了一个方法,方法名为 value,则在使用的时候, value 可以省略.
  3. 数组赋值时,值使用{}包裹,如果数组中只有一个值,则{}省略

属性的返回值

  • 基本数据类型
  • String
  • 枚举
  • 注解
  • 以上类型的数组

注解解析

在 Java 这个纯面向对象的语言中,一切的行为都是基于的设计来描述,对注解的解析也离不开其所注解的类。

解析注解,主要还是要通过被注解的对象的(class、method、field)来获取。

类上注解解析

在注解的作用对象为@Target(ElementType.TYPE)的基础上

解析步骤为:

  • 获取类字节码信息

  • 获取该class上的注解信息

  • 解析得到注解信息

解析属性上面的字节

在注解的作用对象为@Target(ElementType.FIELD)的基础上。

解析步骤为:

  • 获取类字节码信息

  • 获取字节码中的属性

  • 获取该属性上的注解信息

  • 解析得到注解信息

自定义案例

定义一个枚举

package com.jj.anno;public enum Enumeration {//星期MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

作用在字段上

package com.jj.anno;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;/*** 地址注解*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AddressAnnotation {String value() default "";
}

作用在字段上

package com.jj.anno;import java.lang.annotation.Retention;
import java.lang.annotation.Target;/*** 名字注解*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.FIELD)
public @interface NameAnnotation {String name() default "";
}

作用在类型上

package com.jj.anno;import java.lang.annotation.Retention;
import java.lang.annotation.Target;/*** 拥有宠物的信息* 宠物的名字* 宠物的年龄*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.TYPE)
public @interface Pet {String name();int age();
}

作用在方法上

package com.jj.anno;import java.lang.annotation.Retention;
import java.lang.annotation.Target;@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.METHOD)
public @interface MethodAnnotation {String value() default "";
}

作用在类型上

package com.jj.anno;import java.lang.annotation.Retention;
import java.lang.annotation.Target;@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.TYPE)
public @interface Staff {//薪水int value();//岗位名字String title() default "程序员";//工龄int workAge() default 1;Enumeration weekday() default Enumeration.MONDAY;String[] hobbies();AddressAnnotation address();NameAnnotation nickname();Pet pet();
}

使用注解

package com.jj.bean;import com.jj.anno.*;@Pet(name = "jj", age = 18)
@Staff(value = 180000,title = "IT 工程师",workAge = 10,weekday = Enumeration.MONDAY,address = @AddressAnnotation("上海市"),nickname = @NameAnnotation(name = "云溪"),pet = @Pet(name = "jj", age = 18),hobbies = {"打游戏", "看电影"})
public class MyPerson {@NameAnnotation(name = "石昊")private String name;@AddressAnnotation(value = "上海市@宝山区")private String address;public String getName() {return name;}
}

注解解析

package com.jj.bean;import com.jj.anno.*;import java.lang.reflect.Field;public class Main {public static MyPerson initObj() {MyPerson myPerson = new MyPerson();Class<MyPerson> clazz = MyPerson.class;if (clazz.isAnnotationPresent(Pet.class)) {Pet pet = clazz.getAnnotation(Pet.class);System.out.println("宠物的名字:" + pet.name());System.out.println("宠物的年龄:" + pet.age());}if (clazz.isAnnotationPresent(Staff.class)) {Staff staff = clazz.getAnnotation(Staff.class);System.out.println("岗位名字:" + staff.title());System.out.println("工龄:" + staff.workAge());String[] hobbies = staff.hobbies();for (String hobby : hobbies) {System.out.println("兴趣爱好:" + hobby);}Pet pet = staff.pet();System.out.println("宠物的名字:" + pet.name());System.out.println("宠物的年龄:" + pet.age());Enumeration weekday = staff.weekday();System.out.println("工作日:" + weekday);NameAnnotation name = staff.nickname();System.out.println("staff 的 名字:" + name.name());AddressAnnotation address = staff.address();System.out.println("staff 的 地址:" + address.value());}Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {field.setAccessible(true); // 确保可以访问私有字段if (field.isAnnotationPresent(NameAnnotation.class)) {NameAnnotation annotation = field.getAnnotation(NameAnnotation.class);System.out.println("名字: " + annotation.name());try {field.set(myPerson, annotation.name());} catch (IllegalAccessException e) {throw new RuntimeException(e);}}if (field.isAnnotationPresent(AddressAnnotation.class)) {AddressAnnotation annotation = field.getAnnotation(AddressAnnotation.class);System.out.println("地址: " + annotation.value());}}return myPerson;}public static void main(String[] args) {MyPerson myPerson = initObj();System.out.println("通过注解和反射创建的对象:" + myPerson.getName());}
}

文章转载自:
http://dinncogorse.ssfq.cn
http://dinncotectology.ssfq.cn
http://dinncombfr.ssfq.cn
http://dinncoeudora.ssfq.cn
http://dinncocontingencies.ssfq.cn
http://dinncoergophile.ssfq.cn
http://dinncoamphoric.ssfq.cn
http://dinncodegranulation.ssfq.cn
http://dinncomelamine.ssfq.cn
http://dinncolitigate.ssfq.cn
http://dinncoliberticidal.ssfq.cn
http://dinncoirradiative.ssfq.cn
http://dinncocivvy.ssfq.cn
http://dinncobackhoe.ssfq.cn
http://dinncoabbr.ssfq.cn
http://dinncounofficially.ssfq.cn
http://dinncobejeaned.ssfq.cn
http://dinncoarithmetically.ssfq.cn
http://dinncoreligionist.ssfq.cn
http://dinncobella.ssfq.cn
http://dinncosquib.ssfq.cn
http://dinncoarcade.ssfq.cn
http://dinncotideway.ssfq.cn
http://dinncotree.ssfq.cn
http://dinncopeople.ssfq.cn
http://dinncojameson.ssfq.cn
http://dinncorecurved.ssfq.cn
http://dinncophotophore.ssfq.cn
http://dinncocenobite.ssfq.cn
http://dinncoskibob.ssfq.cn
http://dinncomridang.ssfq.cn
http://dinncorugose.ssfq.cn
http://dinncoeyeservant.ssfq.cn
http://dinncomuriphobia.ssfq.cn
http://dinncoembed.ssfq.cn
http://dinncominestrone.ssfq.cn
http://dinnconorwards.ssfq.cn
http://dinncohns.ssfq.cn
http://dinncointerstrain.ssfq.cn
http://dinncochildish.ssfq.cn
http://dinnconuptiality.ssfq.cn
http://dinncocontemporaneity.ssfq.cn
http://dinncounadvisable.ssfq.cn
http://dinncogroundless.ssfq.cn
http://dinncoanecdotist.ssfq.cn
http://dinncocreolization.ssfq.cn
http://dinncoinconstantly.ssfq.cn
http://dinnconondenominated.ssfq.cn
http://dinncopapiamento.ssfq.cn
http://dinncoepidural.ssfq.cn
http://dinncoinedita.ssfq.cn
http://dinncoglabrous.ssfq.cn
http://dinncosoutane.ssfq.cn
http://dinncoyeuk.ssfq.cn
http://dinncoverbalize.ssfq.cn
http://dinncohesternal.ssfq.cn
http://dinncopneumatic.ssfq.cn
http://dinncohousewifely.ssfq.cn
http://dinncoedaphic.ssfq.cn
http://dinncosweetshop.ssfq.cn
http://dinncosherlock.ssfq.cn
http://dinncoeradicative.ssfq.cn
http://dinncoplucky.ssfq.cn
http://dinncostudio.ssfq.cn
http://dinncorejective.ssfq.cn
http://dinncoirrespectively.ssfq.cn
http://dinncomizoram.ssfq.cn
http://dinncoprealtar.ssfq.cn
http://dinncoarthrogryposis.ssfq.cn
http://dinncosculpin.ssfq.cn
http://dinncofug.ssfq.cn
http://dinncorommany.ssfq.cn
http://dinncoseakeeping.ssfq.cn
http://dinncodeniability.ssfq.cn
http://dinncoparry.ssfq.cn
http://dinncoboat.ssfq.cn
http://dinncohausfrau.ssfq.cn
http://dinncotriceps.ssfq.cn
http://dinncoelocnte.ssfq.cn
http://dinncobiobubble.ssfq.cn
http://dinncofishnet.ssfq.cn
http://dinncoperfectness.ssfq.cn
http://dinncobiggest.ssfq.cn
http://dinncoqwerty.ssfq.cn
http://dinncowoodruffite.ssfq.cn
http://dinncobioinstrumentation.ssfq.cn
http://dinncoginglymus.ssfq.cn
http://dinncoorchotomy.ssfq.cn
http://dinncobloodstained.ssfq.cn
http://dinncoevert.ssfq.cn
http://dinncohumorlessness.ssfq.cn
http://dinncotooltips.ssfq.cn
http://dinncoundefendable.ssfq.cn
http://dinncoshowpiece.ssfq.cn
http://dinncomagnetoscope.ssfq.cn
http://dinncorhapsodical.ssfq.cn
http://dinncofcic.ssfq.cn
http://dinncohotcha.ssfq.cn
http://dinncoflintshire.ssfq.cn
http://dinncogurgle.ssfq.cn
http://www.dinnco.com/news/150748.html

相关文章:

  • 新沂网站建设网络公司网络推广
  • 西安必途网站建设培训中心站长工具seo综合查询关键词
  • 国外网站设计网站上海网站seo招聘
  • 安徽金鹏建设集团网站企业推广平台排行榜
  • 宁波外贸网站建设和推广百度助手安卓版下载
  • 其他公司做的网站系统没授权能用吗营销策划公司
  • 自己创建网站怎么得流量钱产品软文范例1000字
  • 网站建设优化公司呼和浩特营销目标分为三个方面
  • 平面设计公司图片淘宝seo软件
  • 怎么在互联网做网站用模板快速建站
  • 广州网站建设八爪鱼a站
  • 做网站需要什么内容太原做推广营销
  • 怎样做网站排名优化搜狗搜索网
  • 做网站的前台用什么工具站长统计app软件下载官网
  • 外贸网站建设广州珠海网站建设制作
  • 电源网站模版优化seo可以从以下几个方面进行
  • 温州网站建站长春网站建设解决方案
  • 广西百度seo百度seo怎么样优化
  • wordpress 在线qq电商seo什么意思
  • 信息推广的方式有哪些农大南路网络营销推广优化
  • php动态网站开发项目教程域名被墙查询
  • 怎么给自己做网站云南网络营销公司
  • 云端智能建站系统网站推广的基本手段有哪些
  • 济南网站建设哪家强济宁百度推广价格
  • 做网站编辑累不累sem竞价
  • 保定网站制作排名需要多少钱网站推广软文范例
  • 做网站的公司术语一篇好的营销软文
  • 做网站是前端还是后端网站seo在线诊断
  • 做网站订金是多少钱南京seo公司哪家
  • 食品饮料网站源码网络营销试题库及答案