-
枚举简介:
枚举也是一个类,同样具有类的所有成员。使用枚举类型可以保证数据信息的“局限性” “安全性” “确定性”。- 局限性:枚举在定义的时候,可以定义指定范围,在使用枚举类型,或者使用枚举类型来赋值时,被赋值的对象中的属性是不能发送改变的,除非被对象内部成员属性发送改变。但尽管如此,定义好的枚举类型,还是不会发生影响。
- 安全性:在实体类型中定义了枚举类型成员,那么该成员只能使用枚举类型中被定义好的取值范围,保证了数据交互拒绝了非法数据的传入,不能随意更改。(如:定义性别类型)
- 确定性:枚举类中的成员一旦定义,是无法从外边进行修改,只能调用。
-
使用场景:
-----在现实中具有确定性的值,如定义性别 ,定义异常 ,定义国籍省份地址等。定义枚举时,尽可能的不要选择定义范围广,有变化,带有随机性的。
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 +
'}';
}
}
注解
-
注解简介:
- 注解是JDK1.5的新特性
- 注解相当于一种标记,是给类的组成部分,可以给类携带一些额外信息。
- 注解以加在包,类,字段,方法,方法参数以及局部变量上。
- 注解是给编译器或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();
}
}
}



