ArrayList后尖括号可以规定集合中元素的类型,如果写入不是一个类型的话,编译器会报错,这样防止了强制类型转换带来的逻辑错误。
泛型定义编译完了以后,参数类型自动舍弃,意思就是虚拟机不知道你所规定的泛型类型了,称之为去类型化。
泛型可以通过反射方法调用函数加入集合中不是泛型所指定的成员变量,说明泛型作用范围在编译阶段不是执行阶段。
泛型通配符,Collection<?> collection.无论泛型指定的集合类型是什么,都有.size方法。然后可以通过for迭代输出。
Map.Entry可以返回一个集合实例化的对象,使我们不必再得到关键字以后再返回MAP进行查找,而直接操作集合中的一个对象。
// TODO Auto-generated method stub
System.out.println(
ClassLoaderTest.class.getClassLoader().getClass().getName()
);
System.out.println(
System.class.getClassLoader()
);
System.out.println("xxx");
ClassLoader loader = ClassLoaderTest.class.getClassLoader();
while(loader != null){
System.out.println(loader.getClass().getName());
loader = loader.getParent();
}
System.out.println(loader);
//System.out.println(new ClassLoaderAttachment().toString());
System.out.println("xxx2");
Class clazz = new MyClassLoader("itcastlib").loadClass("cn.itcast.day2.ClassLoaderAttachment");
Date d1 = (Date)clazz.newInstance();
System.out.println(d1);
类加载器有委托机制,加载器都是向父类查找,查找不到返回。
自己编写的类加载器必须继承ClassLoader,并应覆盖findClass方法。用byte []b = loadClassData(name);return defineClass(name,b,0,b.length)传递字节数组变成一个类。
泛型定义编译完了以后,参数类型自动舍弃,意思就是虚拟机不知道你所规定的泛型类型了,称之为去类型化。
泛型可以通过反射方法调用函数加入集合中不是泛型所指定的成员变量,说明泛型作用范围在编译阶段不是执行阶段。
泛型通配符,Collection<?> collection.无论泛型指定的集合类型是什么,都有.size方法。然后可以通过for迭代输出。
Map.Entry可以返回一个集合实例化的对象,使我们不必再得到关键字以后再返回MAP进行查找,而直接操作集合中的一个对象。
泛型方法引用类型不能是基本类型。
public class GenDemo {
public static void main(String[] args){
//定义泛型类Gen的一个Integer版本
Gen<Integer> intOb=new Gen<Integer>(88);
intOb.showType();
int i= intOb.getOb();
System.out.println("value= " + i);
System.out.println("----------------------------------");
//定义泛型类Gen的一个String版本
Gen<String> strOb=new Gen<String>("Hello Gen!");
strOb.showType();
String s=strOb.getOb();
System.out.println("value= " + s);
}
}
类加载机制getClassLoader方法。
// TODO Auto-generated method stub
System.out.println(
ClassLoaderTest.class.getClassLoader().getClass().getName()
);
System.out.println(
System.class.getClassLoader()
);
System.out.println("xxx");
ClassLoader loader = ClassLoaderTest.class.getClassLoader();
while(loader != null){
System.out.println(loader.getClass().getName());
loader = loader.getParent();
}
System.out.println(loader);
//System.out.println(new ClassLoaderAttachment().toString());
System.out.println("xxx2");
Class clazz = new MyClassLoader("itcastlib").loadClass("cn.itcast.day2.ClassLoaderAttachment");
Date d1 = (Date)clazz.newInstance();
System.out.println(d1);
}
通配符
public void addList(List<? super B> list)
{
list.add(new D()); //编译通过
}
类加载器有委托机制,加载器都是向父类查找,查找不到返回。
自己编写的类加载器必须继承ClassLoader,并应覆盖findClass方法。用byte []b = loadClassData(name);return defineClass(name,b,0,b.length)传递字节数组变成一个类。