枚举与注解
枚举
1.自定义枚举
如果枚举只有单个成员,则可以作为单例模式的实现方式
public class test{
public static void main(String[] args) { Season spring = Season.spring;System.out.println(spring);spring.show();System.out.println(spring.getSeasonName());
}
}
//枚举类
class Season{//1.提供类的属性的私有,不可修改private final String seasonName;private final String seasonDesc;//声明为final的属性,在构造器中初始化private Season(String seasonName, String seasonDesc){this.seasonDesc = seasonDesc;this.seasonName = seasonName;}//通过公共的方法来调用属性public String getSeasonName(){return seasonName;}public String getSeasonDesc(){return seasonDesc;}//创建枚举类对象:将类的对象声明为public static finalpublic static final Season spring = new Season("spring", "万物复苏");public static final Season summer = new Season("summer", "烈日炎炎");public static final Season autumn = new Season("autumn", "秋高气爽");public static final Season winter = new Season("winter", "寒冬来临");public String toString(){return "Season [ Name: " + seasonName + ", Desc: " + seasonDesc + "]";}public void show(){System.out.println("又是这个季节");}
}
2.使用enum关键字定义枚举类
enum关键字是JDK1.5新增的
枚举类的主要方法
public class test1{
public static void main(String[] args) { Season1 spring = Season1.spring;System.out.println(spring); spring.show();System.out.println(spring.getSeasonName()); System.out.println(); //1.枚举的values()方法Season1[] seasons = Season1.values();for(int i = 0;i < seasons.length;i++){System.out.println(seasons[i]); } System.out.println(); //2.枚举的valueof(String name)方法:要求传入的形参name必须是枚举对象的名字,否则报异常IllegalArgumentExceptionString str = "spring";Season1 sea = Season1.valueOf(str);System.out.println(sea);
}
}
//枚举类
enum Season1{spring ("spring", "万物复苏"),summer ("summer", "烈日炎炎"),autumn ("autumn", "秋高气爽"),winter ("winter", "寒冬来临");private final String seasonName; private final String seasonDesc;private Season1(String seasonName, String seasonDesc){this.seasonDesc = seasonDesc;this.seasonName = seasonName;}public String getSeasonName(){return seasonName;}public String getSeasonDesc(){return seasonDesc;}public String toString(){ return "Season [ Name: " + seasonName + ", Desc: " + seasonDesc + "]"; }public void show(){System.out.println("又是这个季节");}
}
3.实现接口的枚举类
- 可以让枚举类的对象调用同一套被重写的抽象方法,执行的效果相同
例:
public class test1{
public static void main(String[] args) { Season1 spring = Season1.spring;System.out.println(spring);spring.show();System.out.println(spring.getSeasonName());System.out.println();
}
}
enum Season1{spring ("spring", "万物复苏"),summer ("summer", "烈日炎炎"),autumn ("autumn", "秋高气爽"),winter ("winter", "寒冬来临");private final String seasonName;private final String seasonDesc;private Season1(String seasonName, String seasonDesc){this.seasonDesc = seasonDesc;this.seasonName = seasonName;}public String getSeasonName(){return seasonName;}public String getSeasonDesc(){return seasonDesc;}public String toString(){return "Season [ Name: " + seasonName + ", Desc: " + seasonDesc + "]";}public void show(){System.out.println("又是这个季节");}
}
- 可以让不同的枚举类的对象调用被重写的抽象方法,执行的效果不同(相当于让每个对象重写自己的抽象方法)
例:
public class test2{
public static void main(String[] args) { Season1 spring = Season1.spring;System.out.println(spring);spring.show();System.out.println(spring.getSeasonName());System.out.println();
}interface info{void show();
}
enum Season1 implements info{spring ("spring", "万物复苏"){public void show(){System.out.println("春天到了!"); }},summer ("summer", "烈日炎炎"){public void show(){System.out.println("夏天到了!");}},autumn ("autumn", "秋高气爽"){public void show(){System.out.println("\u79CB天到了!");}},winter ("winter", "寒冬来临"){public void show(){System.out.println("冬天到了!");}};private final String seasonName;private final String seasonDesc;private Season1(String seasonName, String seasonDesc){this.seasonDesc = seasonDesc;this.seasonName = seasonName;}public String getSeasonName(){return seasonName;}public String getSeasonDesc(){return seasonDesc;}public String toString(){return "Season1 [ Name: " + seasonName + ", Desc: " + seasonDesc + "]";}
}
}
注释(Annotation)
-
从JDK5.0开始增加对元数据的支持,即注解(代码中的特殊标记)
-
可用于修饰包,类,构造器,方法,成员变量,参数,局部变量的声明,保存在注解的"name=value"对中
-
能被用来为程序元素(类,方法等)设置元数据
4.内置的基本注释类型(3个)
@Override
限定重写父类方法,只能用于方法
例:
public class test{public static void main(String[] args) { Person p = new Student();p.info();}
}
class Student extends Person{@Overridepublic void info(){System.out.println("学生表演吃瓜!");}
}
class Person{//个人信息private String name; //姓名private int age; //年龄private String sex; //性别public Person(String name,int age,String sex){super();this.name = name;this.age = age;this.sex = sex;}public Person(){super();}public void info(){System.out.println("人\u8868\u6F14吃瓜!");}
}
@Deprecated
用于表示某个程序元素(类,方法等)已过时
例:
public class test6 {public static void main(String[] args) { Person p = new Student();p.info();}
}
class Student extends Person{public void info(){System.out.println("学生表演吃瓜!");}
}
@Deprecated
class Person{//个人信息private String name; //姓名private int age; //年龄private String sex; //性别public Person(String name,int age,String sex){super();this.name = name;this.age = age;this.sex = sex;}public Person(){super();}public void info(){System.out.println("人\u8868\u6F14吃瓜!");}
}
@SuppressWarnings
抑制编译器警告
import java.util.ArrayList;
import java.util.List;
public class test {public static void main(String[] args) { @SuppressWarnings({"rawtypes","unused"})//抑制没有使用泛型,没有使用的警告List list = new ArrayList();}
}
5.自定义注解
例:
public @interface test7 {String value() default "hello";
}
//使用
public class test6 {@test7(value = "qwerq")public static void main(String[] args) { }
}
6.元注解(4个)
修饰其他注解的定义,该注解具体有
@Retention
只能修饰一个注解的定义,指定该注解可以保留多长时间,@Retention中有一个RetentionPolicy
类型的成员变量,使用时,必须给这个成员变量指定值(值:
- SOURCE(编译器直接放弃这种注解)
- CLASS(编译器将注释记录到class文件中,运行时,JVM不保留注解,这是默认值)
- RUNTIME(编译器将注释记录到class文件中,运行时,JVM保留注解,程序可以通过反射获取该注解))
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.CLASS)
public @interface test7 {String value() default "hello";
}
@Target
指定被修饰的注解能用于哪些程序元素,@Target中有一个value
的成员变量
@Documented
将被javadoc工具提取成文件,@Retention
值必须是RUNTIME
@Inherited
被它修饰的注解将具有继承性,某个类使用了此注解,那它的子类将自动具有该注释
感谢大家的支持,关注,评论,点赞!
参考资料:
尚硅谷宋红康20天搞定Java基础中部