泛型
为什么需要泛型
当我们将一个对象放入集合中,集合不会记住此对象的类型,当再次从集合中取出此对象时,该对象的编译类型变成了Object类型,其运行时类型依然为其本身类型 如果没有泛型,取出集合元素时需要人为的强制类型转换为具体的目标类型,容易出现“java.lang.ClassCastException”异常 泛型【参数化类型】 通配符:【?】 可以接收任意的泛型类型,只能输出不能修改 泛型上限: 【? extends 类】 只能设置具体类或者子类 泛型下限: 【? super 类】 只能设置具体类或者父类
public class Node < T > {
private T data;
public Node ( ) {
}
public Node ( T data) {
this . data = data;
}
public T getData ( ) {
return data;
}
public void setData ( T data) {
this . data = data;
}
}
public class GenericDemo {
@Test
public void test1 ( ) {
List < String > list = new ArrayList < > ( ) ;
list. add ( "lily" ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
}
}
@Test
public void test2 ( ) {
Node < String > stringNode = new Node < > ( "lily" ) ;
Node < Integer > intNode = new Node < > ( 10 ) ;
System . out. println ( stringNode. getData ( ) ) ;
System . out. println ( intNode. getData ( ) ) ;
}
public static < T > T [ ] func ( T [ ] array, int i, int t) {
T temp = array[ i] ;
array[ i] = array[ t] ;
array[ t] = temp;
return array;
}
@Test
public void test3 ( ) {
String [ ] arrays = { "vince" , "jack" , "Tom" , "lily" } ;
String [ ] strs = func ( arrays, 0 , 1 ) ;
System . out. println ( Arrays . toString ( strs) ) ;
}
public static void getUpperNumberData ( Node < ? extends Number > data) {
System . out. println ( "data :" + data. getData ( ) ) ;
}
@Test
public void test4 ( ) {
Node < Number > n1 = new Node < > ( 10 ) ;
Node < Integer > n2 = new Node < > ( 20 ) ;
getUpperNumberData ( n1) ;
getUpperNumberData ( n2) ;
}
public static void getData2 ( Node < ? > node) {
System . out. println ( node. getData ( ) ) ;
}
@Test
public void test5 ( ) {
Map < Integer , String > map = new HashMap < > ( ) ;
map. put ( 1 , "vince" ) ;
map. put ( 2 , "tom" ) ;
Set < Map. Entry < Integer , String > > entrySet = map. entrySet ( ) ;
for ( Map. Entry entry: entrySet) {
System . out. println ( entry. getKey ( ) + "-" + entry. getValue ( ) ) ;
}
}
}
正则表达式
@Test
public void test2 ( ) {
Pattern p = Pattern . compile ( "a*b" ) ;
Matcher matcher = p. matcher ( "sb" ) ;
boolean b = matcher. matches ( ) ;
System . out. println ( b) ;
}
@Test
public void test3 ( ) {
String s = "5201314" ;
boolean b = s. matches ( "[0-9]+" ) ;
boolean b1 = s. matches ( "\\d+" ) ;
System . out. println ( b+ "-" + b1) ;
}
枚举
枚举就是要让某个类型的变量的取值只能为若干个固定值中的一个,否则编译器就会报错,枚举可以让编译器在编译时就可以控制源程序赋给的非法值,使用普通变量的方式在开发阶段无法实现这一目标
public interface Info {
public int getColor ( ) ;
}
public enum Color implements Info {
RED ( 10 ) {
@Override
public String getColor2 ( ) {
return "red" ;
}
} ,
BLUE ( ) {
@Override
public String getColor2 ( ) {
return "blue" ;
}
} ,
GREEN ( 20 ) {
@Override
public String getColor2 ( ) {
return "green" ;
}
} ;
private int color;
private Color ( ) {
System . out. println ( "无参构造器" ) ;
}
private Color ( int color) {
this . color = color;
System . out. println ( "有参构造器" ) ;
}
@Override
public int getColor ( ) {
return color;
}
public abstract String getColor2 ( ) ;
}
public class EnumDemo {
public static final int RED = 0x1 ;
public static final int GREEN = 0x2 ;
public static final int BLUE = 0x3 ;
public int color;
@Test
public void test1 ( ) {
color = RED;
color = 4 ;
}
public Color colorEnum;
@Test
public void test2 ( ) {
colorEnum = Color . GREEN;
System . out. println ( colorEnum) ;
System . out. println ( colorEnum. name ( ) ) ;
System . out. println ( colorEnum. ordinal ( ) ) ;
System . out. println ( colorEnum. toString ( ) ) ;
Color [ ] values = Color . values ( ) ;
System . out. println ( Arrays . toString ( values) ) ;
}
@Test
public void test3 ( ) {
EnumSet < Color > set = EnumSet . allOf ( Color . class ) ;
for ( Color c: set) {
System . out. println ( c) ;
}
EnumMap < Color , String > map = new EnumMap < Color , String > ( Color . class ) ;
map. put ( Color . RED, "red" ) ;
map. put ( Color . GREEN, "green" ) ;
}
@Test
public void test4 ( ) {
System . out. println ( Color . RED. getColor ( ) ) ;
System . out. println ( Color . RED. getColor2 ( ) ) ;
}
@Test
public void test5 ( ) {
Singleton . SINGLETON. method ( ) ;
}
}
public enum Singleton {
SINGLETON;
public void method ( ) {
System . out. println ( "method" ) ;
}
}
注解【Annotation】
@Override 覆盖超类中的方法 @Deprecated 以过时方法 @SuppressWarnings 关闭不当的编译器警告信息 自定义Annotation
编写注解 在类上应用注解 对应用了注解的类进行反射操作的类 public @interface Annotation名称 访问控制权限 @Documented注解 文档化,可以在生成doc文档的时候添加注解 @Target注解 一个Annotation的使用范围 @Inherited注解 一个Annotation是否允许被子类继承下来
@Documented
@Retention ( RetentionPolicy . RUNTIME)
@Target ( ElementType . TYPE)
@Inherited
public @interface MyAnnotation {
public String name ( ) ;
public int age ( ) default 2 ;
public String [ ] like ( ) ;
public Color color ( ) ;
}
public enum Color {
RED, GREEN, YELLOW;
}
@MyAnnotation ( name = "bin" , like = { "金鱼" , "鲫鱼" , "鲤鱼" } , color = Color . GREEN)
public class Cat {
private String name;
private int age;
private Color color;
private String [ ] like;
public String [ ] getLike ( ) {
return like;
}
public void setLike ( String [ ] like) {
this . like = like;
}
public Color getColor ( ) {
return color;
}
public void setColor ( Color color) {
this . color = color;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
", color=" + color +
", like=" + Arrays . toString ( like) +
'}' ;
}
@Deprecated
public String printInfo ( ) {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
public Cat ( ) {
}
public Cat ( String name, int age) {
this . name = name;
this . age = age;
}
}
@SuppressWarnings ( "all" )
public class AnnotationDemo {
@Test
public void test2 ( ) {
Class < Cat > catClass = Cat . class ;
MyAnnotation annotation = catClass. getAnnotation ( MyAnnotation . class ) ;
String name = annotation. name ( ) ;
int age = annotation. age ( ) ;
Color color = annotation. color ( ) ;
String [ ] like = annotation. like ( ) ;
try {
Cat cat = catClass. newInstance ( ) ;
cat. setName ( name) ;
cat. setAge ( age) ;
cat. setLike ( like) ;
cat. setColor ( color) ;
System . out. println ( cat) ;
} catch ( InstantiationException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
}
}
@Test
public void test1 ( ) {
Cat cat = new Cat ( "miaomiao" , 3 ) ;
cat. printInfo ( ) ;
List list = new ArrayList ( ) ;
list. add ( "vince" ) ;
list. add ( 10 ) ;
list. add ( 10.1f ) ;
}
}