枚举 注解 MAP
标签(空格分隔): Java
枚举类
一、 枚举类的使用
- 枚举类的理解:类的对象只有有限个数,确定的。我们称此类为枚举类
- 当需要定义一组常量时,强烈建议使用枚举类
- 如果枚举类只用一个对象,则可以作为单例模式的实现方式
二、如何定义枚举类
方式一:jdk5.0之前,自定义枚举类
方法二:jdk5.0,可以使用enum关键自定义枚举类
三、Enum类的主要方法:
values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的
枚举值。
valueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符
串必须是枚举类对象的“名字”。如不是,会有运行时异常:
IllegalArgumentException。
toString():返回当前枚举类对象常量的名称
四、使用enum关键字定义的枚举类实现接口的情况
情况一:实现接口,在enum类中实现抽象方法
情况二:让枚举类的对象分别实现接口中的抽象方法
//方法一:
package newday4;
public class SeasonTest {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
}
}
//自定义
class Season{
//1.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//2.私有化类的构造器,并给对象属性赋值
private Season(String seasonName,String seasonDesc){
this.seasonDesc = seasonDesc;
this.seasonName = seasonName;
}
//3.提供当前枚举类的多个对象:public static final的
public static final Season SPRING = new Season("春天","AAA");
public static final Season SUMMER = new Season("春天","BBB");
public static final Season AUTUMN = new Season("春天","CCC");
public static final Season WINTER = new Season("春天","DDD");
//4.其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//4.其他诉求2:提供toString()
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
--
//enum类的使用
package newday4;
public class SeasonTest1 {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
}
}
//使用enum关键字
enum Season1{
//1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象";"结束
SPRING("春天","AAA"),
SUMMER("夏天","BBB"),
AUTUMN("秋天","CCC"),
WINTER("冬天","DDD");
//1.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//2.私有化类的构造器,并给对象属性赋值
private Season1(String seasonName,String seasonDesc){
this.seasonDesc = seasonDesc;
this.seasonName = seasonName;
}
//4.其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//4.其他诉求2:提供toString()
/*@Override
public String toString() {
return "Season1{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}*/
}
注解
集合
package newday5;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class CollectionTest {
@Test
public void test1(){
Collection coll = new ArrayList();//有序的
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
/*coll.add(new Person("Jerry",20));*/
// Person p = new Person("Jerry",20);
// coll.add(p);
coll.add(new Person("Jerry",20));
//contains(Object obj):判断当前集合中是否包含obj
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));//判断内容
// System.out.println(coll.contains(p));//ture
System.out.println(coll.contains(new Person("Jerry",20)));
}
@Test
public void test2(){
//remove(object obj):从当前集合中移除obj元素
Collection coll = new ArrayList();//有序的
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(new Person("Jerry",20));
coll.add(false);
// coll.remove(123);
System.out.println(coll.contains(123));
System.out.println(coll);
coll.remove(new Person("Jerry",20));
System.out.println(coll);
//4. removeAll(collection coll1):差集:从当前集合中移除coll1中所有的元素
Collection coll1 = Arrays.asList(123,4567);
coll.removeAll(coll1);
System.out.println(coll);
}
@Test
public void test3(){
Collection coll = new ArrayList();//有序的
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(new Person("Jerry",20));
coll.add(false);
System.out.println(coll);
//5. retainAll(Collection coll1)交集:获取当前集合和coll1集合的交集,并返回给当前集合
/*Collection coll1 = Arrays.asList(123,456,789);
coll.retainAll(coll1);
System.out.println(coll);*/
//6. equal(Object obj):要想返回ture,需要当前集合和形参集合的元素相同
Collection coll1 = new ArrayList();//有序的
coll1.add(123);
coll1.add(456);
coll1.add(new String("Tom"));
coll1.add(new Person("Jerry",20));
coll1.add(false);
System.out.println(coll1);
System.out.println(coll.equals(coll1));
}
@Test
public void test4(){
Collection coll = new ArrayList();//有序的
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(new Person("Jerry",20));
coll.add(false);
//7.hashCode():返回当前对象的哈希值
System.out.println(coll.hashCode());
//8.集合——————》数组:toArray()
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
//拓展:数组————》集合:调用Arrays类的静态方法alist()
List<String> list = Arrays.asList(new String[]{"AA","BB","CC"});
System.out.println(list);
}
}