一、泛型
泛型在Java中的应用非常广泛,最常见则是在集合容器中,先看下基础用法:
public class Generic01 {public static void main(String[] args) {Map<Integer,String> map = new HashMap<>() ;map.put(88,"hello") ;// map.put("99","world") ; 输入编译错误String value = map.get(88) ;// Integer value = map.get(88) ; 输出编译错误System.out.println("value:"+value);}
}
泛型即可以理解为把数据类型作为参数,即参数化类型
首先设计一个简单的顶层接口,只定义一个callBack方法,和对出入参数的简单逻辑设定
/*** 基础接口设计* @param <R> 返参类型* @param <V> 入参类型*/
interface CallBack<R,V> {/*** 回调方法:V 方法入参 ,R 方法返回值*/R callBack (V v) ;
}
为了实现具体的业务,再基于顶层接口向下做扩展,
这里声明两个扩展接口,作为具体业务类的接口
/*** 扩展接口设计01*/
interface ExtCallBack01<R extends Integer,V extends List<String>>extends CallBack<Integer,List<String>> {@OverrideInteger callBack (List<String> list) ;
}
/*** 扩展接口设计01*/
interface ExtCallBack02<R extends Boolean,V extends Map<String,Long>>extends CallBack<Boolean,Map<String,Long>> {@OverrideBoolean callBack (Map<String,Long> map) ;
}
可以通过扩展接口去设计具体的业务类,提高程序的灵活可扩展性:
public class Generic02 {public static void main(String[] args) {new ExtCallBack01<Integer,List<String>>(){@Overridepublic Integer callBack(List<String> list) {list.add("hello");return list.size();}};new ExtCallBack02<Boolean,Map<String,Long>> (){@Overridepublic Boolean callBack(Map<String, Long> map) {map.put("Fire",119L) ;return map.size()>0 ;}} ;}
}
二、反射机制
反射机制可以在程序运行时获取类的完整结构信息,并且可以动态的操作属性和方法等。
反射的类库
- java.lang.Class:Class类
- java.lang.reflect.Constructor:构造器
- java.lang.reflect.Field:属性
- java.lang.reflect.Method:方法
API之Class对象
public static void main(String[] args) throws Exception {// Class对象回去User user1 = new User(1,"name01") ;//方式一Class userClass1 = user1.getClass() ;//方式二Class userClass2 = Class.forName("com.java.reflect.User");//方式三Class userClass3 = User.class ;System.out.println(User.class.getName());System.out.println("userClass1==userClass2?"+(userClass1==userClass2));System.out.println("userClass2==userClass3?"+(userClass2==userClass3));// 类型创建和判断Object object = User.class.newInstance() ;System.out.println("类型:"+(object instanceof User));System.out.println("类型:"+(userClass3.isInstance(user1)));
}
API之Constructor构造器
public static void main(String[] args) throws Exception {Class userClass = User.class ;Constructor[] userConArr = userClass.getConstructors();printCon(userConArr);Constructor privateCon = userClass.getDeclaredConstructor(Integer.class);System.out.println(privateCon);userConArr = userClass.getDeclaredConstructors();printCon(userConArr);Constructor pubCon = userClass.getConstructor(Integer.class,String.class);Object pubUser = pubCon.newInstance(1,"hello") ;// 调用私有构造方法创建对象Constructor priCon = userClass.getDeclaredConstructor(Integer.class);// 忽略private权限修饰符priCon.setAccessible(Boolean.TRUE);Object priUser = priCon.newInstance(2) ;System.out.println(pubUser+"\n"+priUser);
}
public static void printCon (Constructor[] constructors){for (Constructor constructor:constructors){System.out.println(constructor);}
}
API之Field属性
public static void main(String[] args) throws Exception {Class userClass = User.class ;// 获取公共字段Field[] pubArr = userClass.getFields() ;printField(pubArr);// 获取全部字段Field[] fieldArr = userClass.getDeclaredFields() ;printField(fieldArr);// 获取指定字段Field emailField = userClass.getField("email") ;Field nameField = userClass.getDeclaredField("name") ;printField(new Field[]{emailField,nameField});// 创建对象并操作属性Object userObj = userClass.newInstance() ;nameField.setAccessible(Boolean.TRUE);nameField.set(userObj,"world");emailField.set(userObj,"test@email.com");System.out.println("userObj:"+userObj);
}
/*** 打印成员变量信息*/
public static void printField (Field[] fields){for (Field field : fields){System.out.println("声明:"+field);UserAnno userAnno = field.getAnnotation(UserAnno.class) ;System.out.println("注解:"+userAnno.desc());String fieldName = field.getName() ;System.out.println("名称:"+fieldName);Type type = field.getGenericType() ;System.out.println("类型:"+type);}
}
API之Method方法
public static void main(String[] args) throws Exception {Class userClass = User.class ;// 获取所有公共方法[包括父类和Object类方法]Method[] pubMethods = userClass.getMethods() ;printMethod(pubMethods);// 获取全部方法Method[] allMethods = userClass.getDeclaredMethods() ;printMethod(allMethods);// 获取指定方法Method method = userClass.getMethod("parName",String.class) ;printMethod(new Method[]{method});// 调用方法Object userObj = userClass.newInstance() ;Method setId = userClass.getDeclaredMethod("setId", Integer.class);setId.invoke(userObj,99) ;Method setName = userClass.getDeclaredMethod("setName", String.class);setName.invoke(userObj,"java") ;Method sayHi = userClass.getDeclaredMethod("sayHi", String.class);sayHi.setAccessible(Boolean.TRUE);sayHi.invoke(userObj,"c++");System.out.println(userObj);
}
/*** 打印方法信息*/
public static void printMethod (Method[] methods){for (Method method : methods){System.out.println("定义:"+method);System.out.println("命名:"+method.getName());UserAnno userAnno = method.getAnnotation(UserAnno.class) ;if (userAnno != null){System.out.println("注解:"+userAnno.desc());}Type[] paramTypeArr = method.getParameterTypes();for (int i=0 ; i< paramTypeArr.length; i++){System.out.print("参数"+(i+1)+"类型:"+paramTypeArr[i]+" ; ");}System.out.println("参数个数:"+method.getParameterCount());}
}