如果枚举只有单个成员,则可以作为单例模式的实现方式
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 final
public 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("又是这个季节");
}
}
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必须是枚举对象的名字,否则报异常IllegalArgumentException
String 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("又是这个季节");
}
}
例:
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 + "]";
}
}
}
从JDK5.0开始增加对元数据的支持,即注解(代码中的特殊标记)
可用于修饰包,类,构造器,方法,成员变量,参数,局部变量的声明,保存在注解的"name=value"对中
能被用来为程序元素(类,方法等)设置元数据
限定重写父类方法,只能用于方法
例:
public class test{
public static void main(String[] args) {
Person p = new Student();
p.info();
}
}
class Student extends Person{
@Override
public 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吃瓜!");
}
}
用于表示某个程序元素(类,方法等)已过时
例:
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吃瓜!");
}
}
抑制编译器警告
import java.util.ArrayList;
import java.util.List;
public class test {
public static void main(String[] args) {
@SuppressWarnings({"rawtypes","unused"})
//抑制没有使用泛型,没有使用的警告
List list = new ArrayList();
}
}
例:
public @interface test7 {
String value() default "hello";
}
//使用
public class test6 {
@test7(value = "qwerq")
public static void main(String[] args) {
}
}
修饰其他注解的定义,该注解具体有
只能修饰一个注解的定义,指定该注解可以保留多长时间,@Retention中有一个RetentionPolicy
类型的成员变量,使用时,必须给这个成员变量指定值(值:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.CLASS)
public @interface test7 {
String value() default "hello";
}
指定被修饰的注解能用于哪些程序元素,@Target中有一个value
的成员变量
将被javadoc工具提取成文件,@Retention
值必须是RUNTIME
被它修饰的注解将具有继承性,某个类使用了此注解,那它的子类将自动具有该注释
感谢大家的支持,关注,评论,点赞!
参考资料:
尚硅谷宋红康20天搞定Java基础中部