栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础--枚举和注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java基础--枚举和注解

枚举
  • 枚举简介:
    枚举也是一个类,同样具有类的所有成员。使用枚举类型可以保证数据信息的“局限性” “安全性” “确定性”。

    1. 局限性:枚举在定义的时候,可以定义指定范围,在使用枚举类型,或者使用枚举类型来赋值时,被赋值的对象中的属性是不能发送改变的,除非被对象内部成员属性发送改变。但尽管如此,定义好的枚举类型,还是不会发生影响。
    2. 安全性:在实体类型中定义了枚举类型成员,那么该成员只能使用枚举类型中被定义好的取值范围,保证了数据交互拒绝了非法数据的传入,不能随意更改。(如:定义性别类型)
    3. 确定性:枚举类中的成员一旦定义,是无法从外边进行修改,只能调用。
  • 使用场景:
    -----在现实中具有确定性的值,如定义性别 ,定义异常 ,定义国籍省份地址等。定义枚举时,尽可能的不要选择定义范围广,有变化,带有随机性的。

public enum Sex {
    BOY(18),GIRL(16);
    public int age;
    Sex(int age){
        this.age = age;
    }

    public void showAge(){
        System.out.println("age is:" +age);
    }
}

public class TestEnum {
    public static void main(String[] args) {
        Stu stu = new Stu("stu",Sex.GIRL,15);
        Stu stu1 = new Stu("stu1",Sex.GIRL,17);
        //stu2中年龄成员使用枚举中的取值范围,因在Stu实体类中的age成员定义的类型是一致的,所以同样可以使用枚举类型的取值范围
        Stu stu2 = new Stu("stu2",Sex.BOY,Sex.GIRL.age);
        Stu stu3 = new Stu("stu2",Sex.BOY);
        System.out.println(stu); //age为15
        System.out.println(stu1); //age为17
        System.out.println(stu2); //age为16
        System.out.println(stu3); //age为0
    }
}
class Stu{
    private String name;
    private Sex sex;  //使用枚举类型定义性别
    private int age;  //编译时是否报错,要求与实体类的成员类型一致,一致则通过,否则报错.
    public Stu() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
  //      this.age = sex.age; 这一块不影响取值
        this.age = age;
    }

    public Stu(String name, Sex sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public Stu(String name, Sex sex) {
        this.name = name;
        this.sex = sex;
     //        this.age = sex.age;   这一块会直接影响取值
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + ''' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }
}


注解
  • 注解简介:

    1. 注解是JDK1.5的新特性
    2. 注解相当于一种标记,是给类的组成部分,可以给类携带一些额外信息。
    3. 注解以加在包,类,字段,方法,方法参数以及局部变量上。
    4. 注解是给编译器或jvm看的,编译器或jvm可以根据注解来完成对应的功能。
  • 元注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface StuTestAnnotation{
    String name() default " ";
    int age() default 0;
    char sex() default ' ';
}

@StuTestAnnotation(name = "stuClass",age = 15,sex = '男')
public class StudentOfAnnotation {

    @StuTestAnnotation(name = "stuMethod",age = 16,sex = '女')
    public void stuTestAnnotation(){

    }
}

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

public class StudentMain {

    
    public static void main(String[] args) {
        //获取对象
        Class student = StudentOfAnnotation.class;
        //判断类上是否使用StuTestAnnotation注解
        System.out.println("获取类上:");
        if (student.isAnnotationPresent(StuTestAnnotation.class)){
            //根据注解的class对象获得对应的注解对象
            StuTestAnnotation annotation =(StuTestAnnotation) student.getAnnotation(StuTestAnnotation.class);
            System.out.println(annotation.name());
            System.out.println(annotation.age());
            System.out.println(annotation.sex());
        }
        //获得当前对象上使用的所以注解,返回注解数组
        Annotation[] declaredAnnotations = student.getDeclaredAnnotations();
        System.out.println(Arrays.toString(declaredAnnotations));

        System.out.println("获取方法上:");
        try {
            Method method = student.getMethod("stuTestAnnotation");
            StuTestAnnotation annotation = method.getAnnotation(StuTestAnnotation.class);
            System.out.println(annotation.name());
            System.out.println(annotation.age());
            System.out.println(annotation.sex());
            Annotation[] declaredAnnotations1 = method.getDeclaredAnnotations();
            System.out.println(Arrays.toString(declaredAnnotations1));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318573.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号