首页 > 编程知识 正文

java之枚举类和注解,java枚举类的作用

时间:2023-12-27 22:27:52 阅读:326861 作者:GJVK

本文目录一览:

java中的枚举类型指的是什么啊?

在Java中,枚举类型本质上其实就是一个类,枚举中的常量都是该枚举类型的实例。虽然枚举类型有一些限制,比如不能再派生出子枚举类型,不能调用构造函数,不过我们仍然可以在枚举类型中定义构造函数、字段和方法

Java 什么是注解及注解原理详细介绍

1、注解是针对Java编译器的说明。

可以给Java包、类型(类、接口、枚举)、构造器、方法、域、参数和局部变量进行注解。Java编译器可以根据指令来解释注解和放弃注解,或者将注解放到编译后的生成的class文件中,运行时可用。

2、注解和注解类型

注解类型是一种特殊的接口类型,注解是注解注解类型的一个实例。

注解类型也有名称和成员,注解中包含的信息采用键值对形式,可以有0个或多个。

3、Java中定义的一些注解:

@Override 告诉编译器这个方法要覆盖一个超类方法,防止程序员覆盖出错。

@Deprecated 这个标识方法或类(接口等类型)过期,警告用户不建议使用。

@SafeVarargs JDK7新增,避免可变参数在使用泛型化时候警告”执行时期无法具体确认参数类型“,当然,也可以用@SuppressWarnings来避免检查,显然后者的抑制的范围更大。

@SuppressWarnings(value={"unchecked"}) 抑制编译警告,应用于类型、构造器、方法、域、参数以及局部变量。 value是类型数组,有效取值为:

all, to suppress all warnings

boxing, to suppress warnings relative to boxing/unboxing operations

cast, to suppress warnings relative to cast operations

dep-ann, to suppress warnings relative to deprecated annotation

deprecation, to suppress warnings relative to deprecation

fallthrough, to suppress warnings relative to missing breaks in switch statements

finally, to suppress warnings relative to finally block that don't return

hiding, to suppress warnings relative to locals that hide variable

incomplete-switch, to suppress warnings relative to missing entries in a switch statement (enum case)

javadoc, to suppress warnings relative to javadoc warnings

nls, to suppress warnings relative to non-nls string literals

null, to suppress warnings relative to null analysis

rawtypes, to suppress warnings relative to usage of raw types

restriction, to suppress warnings relative to usage of discouraged or forbidden references

serial, to suppress warnings relative to missing serialVersionUID field for a serializable class

static-access, to suppress warnings relative to incorrect static access

static-method, to suppress warnings relative to methods that could be declared as static

super, to suppress warnings relative to overriding a method without super invocations

synthetic-access, to suppress warnings relative to unoptimized access from inner classes

unchecked, to suppress warnings relative to unchecked operations

unqualified-field-access, to suppress warnings relative to field access unqualified

unused, to suppress warnings relative to unused code and dead code

4、注解的定义

使用 @interface 关键字声明一个注解

public @interface MyAnnotation1

注解中可以定义属性

String name default “defval”;

value是注解中的特殊属性

注解中定义的属性如果名称为 value, 此属性在使用时可以省写属性名

例如,声明一个注解:

@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnno1 {

String msg();

int value();

}

java 枚举类型怎么添加注释

枚举类型添加注释是什么意思?

java 注释 有

单行注释 //

多行注释 /* ... */

JavaDoc /** ... */

Java语言中的枚举类型如何使用?

Java语言中的枚举类型的使用方法如下:

用法一:常量;

public enum Color {  

  RED, GREEN, BLANK, YELLOW  

}

用法二:switch;

 enum Signal {

        GREEN, YELLOW, RED

    }

    public class TrafficLight {

        Signal color = Signal.RED;

        public void change() {

            switch (color) {

            case RED:

                color = Signal.GREEN;

                break;

            case YELLOW:

                color = Signal.RED;

                break;

            case GREEN:

                color = Signal.YELLOW;

                break;

            }

        }

    }

用法三:向枚举中添加新方法;

public enum Color {

        RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);

        // 成员变量

        private String name;

        private int index;

        // 构造方法

        private Color(String name, int index) {

            this.name = name;

            this.index = index;

        }

        // 普通方法

        public static String getName(int index) {

            for (Color c : Color.values()) {

                if (c.getIndex() == index) {

                    return c.name;

                }

            }

            return null;

        }

        // get set 方法

        public String getName() {

            return name;

        }

        public void setName(String name) {

            this.name = name;

        }

        public int getIndex() {

            return index;

        }

        public void setIndex(int index) {

            this.index = index;

        }

    }

用法四:覆盖枚举的方法;

public class Test {

    public enum Color {

        RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);

        // 成员变量

        private String name;

        private int index;

        // 构造方法

        private Color(String name, int index) {

            this.name = name;

            this.index = index;

        }

        // 覆盖方法

        @Override

        public String toString() {

            return this.index + "_" + this.name;

        }

    }

    public static void main(String[] args) {

        System.out.println(Color.RED.toString());

    }

}

用法五:实现接口;

public interface Behaviour {

        void print();

        String getInfo();

    }

    public enum Color implements Behaviour {

        RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);

        // 成员变量

        private String name;

        private int index;

        // 构造方法

        private Color(String name, int index) {

            this.name = name;

            this.index = index;

        }

        // 接口方法

        @Override

        public String getInfo() {

            return this.name;

        }

        // 接口方法

        @Override

        public void print() {

            System.out.println(this.index + ":" + this.name);

        }

    }

用法六:使用接口组织枚举。

public interface Food {

    enum Coffee implements Food {

      BLACK_COFFEE, DECAF_COFFEE, LATTE, CAPPUCCINO

   }

  enum Dessert implements Food {

      FRUIT, CAKE, GELATO

  }

}

以上就是Java语言中枚举类型的基本使用方法。

java注解是怎么实现的

注解的使用一般是与java的反射一起使用,下面是一个例子

注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记,以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。

自定义注解及其应用

1)、定义一个最简单的注解

public @interface MyAnnotation {

//......

}

2)、把注解加在某个类上:

@MyAnnotation

public class AnnotationTest{

//......

}

以下为模拟案例

自定义注解@MyAnnotation

1 package com.ljq.test;

2

3 import java.lang.annotation.ElementType;

4 import java.lang.annotation.Retention;

5 import java.lang.annotation.RetentionPolicy;

6 import java.lang.annotation.Target;

7

8 /**

9 * 定义一个注解

10 *

11 *

12 * @author jiqinlin

13 *

14 */

15 //Java中提供了四种元注解,专门负责注解其他的注解,分别如下

16

17 //@Retention元注解,表示需要在什么级别保存该注释信息(生命周期)。可选的RetentionPoicy参数包括:

18 //RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉

19 //RetentionPolicy.CLASS:停留在class文件中,但会被VM丢弃(默认)

20 //RetentionPolicy.RUNTIME:内存中的字节码,VM将在运行时也保留注解,因此可以通过反射机制读取注解的信息

21

22 //@Target元注解,默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括

23 //ElementType.CONSTRUCTOR: 构造器声明

24 //ElementType.FIELD: 成员变量、对象、属性(包括enum实例)

25 //ElementType.LOCAL_VARIABLE: 局部变量声明

26 //ElementType.METHOD: 方法声明

27 //ElementType.PACKAGE: 包声明

28 //ElementType.PARAMETER: 参数声明

29 //ElementType.TYPE: 类、接口(包括注解类型)或enum声明

30

31 //@Documented将注解包含在JavaDoc中

32

33 //@Inheried允许子类继承父类中的注解

34

35

36 @Retention(RetentionPolicy.RUNTIME)

37 @Target({ElementType.METHOD, ElementType.TYPE})

38 public @interface MyAnnotation {

39 //为注解添加属性

40 String color();

41 String value() default "我是林计钦"; //为属性提供默认值

42 int[] array() default {1, 2, 3};

43 Gender gender() default Gender.MAN; //添加一个枚举

44 MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期为1988-2-18");

45 //添加枚举属性

46

47 }

注解测试类AnnotationTest

1 package com.ljq.test;

2

3 /**

4 * 注解测试类

5 *

6 *

7 * @author jiqinlin

8 *

9 */

10 //调用注解并赋值

11 @MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = "我的出身日期为1988-2-18"),color="red", array={23, 26})

12 public class AnnotationTest {

13

14 public static void main(String[] args) {

15 //检查类AnnotationTest是否含有@MyAnnotation注解

16 if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){

17 //若存在就获取注解

18 MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);

19 System.out.println(annotation);

20 //获取注解属性

21 System.out.println(annotation.color());

22 System.out.println(annotation.value());

23 //数组

24 int[] arrs=annotation.array();

25 for(int arr:arrs){

26 System.out.println(arr);

27 }

28 //枚举

29 Gender gender=annotation.gender();

30 System.out.println("性别为:"+gender);

31 //获取注解属性

32 MetaAnnotation meta=annotation.metaAnnotation();

33 System.out.println(meta.birthday());

34 }

35 }

36 }

枚举类Gender,模拟注解中添加枚举属性

1 package com.ljq.test;

2 /**

3 * 枚举,模拟注解中添加枚举属性

4 *

5 * @author jiqinlin

6 *

7 */

8 public enum Gender {

9 MAN{

10 public String getName(){return "男";}

11 },

12 WOMEN{

13 public String getName(){return "女";}

14 }; //记得有“;”

15 public abstract String getName();

16 }

注解类MetaAnnotation,模拟注解中添加注解属性

1 package com.ljq.test;

2

3 /**

4 * 定义一个注解,模拟注解中添加注解属性

5 *

6 * @author jiqinlin

7 *

8 */

9 public @interface MetaAnnotation {

10 String birthday();

11 }

枚举类与注解9:类型注解(JDK8新特性)

类型注解:

关于元注解@Target的参数类型ElementType枚举值多了两个:TYPE_PARAMETER,TYPE_USE

ElementType.TYPE_PARAMETER:表示该注解能写在类型变量的声明语句中(如,泛型声明)

ElementType.TYPE_USE:表示该注解能写在使用类型的任何语句中。

具体举例:

MyAnnotation.java

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。