标签:必须 img href 获得 .class 有趣 array 高效 etc
运行时类型信息使得你可以在程序运行时发现和使用类型信息
使得你从只能在编译期执行面向类型的操作的禁锢中解脱出来,并且可以使用某些强大的程序
RRTI:通过运行时类型信息程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。
class Candy{
static {
System.out.println("loding Candy");
}
}
class Gum extends Candy{
static {
System.out.println("loding Gum");
}
}
class Cookie{
static {
System.out.println("loding Cookie");
}
}
public class SweetShop {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
new Candy();
try {
Class cs=Class.forName("reflectClass.Gum");//输入全限定名
List<Object> list=Arrays.asList(cs.getName(),cs.getSimpleName(),
cs.getCanonicalName(),//返回全限定类名
cs.getSuperclass());//返回其直接基类
cs.getInterfaces();//返回包含的接口
Gum g=(Gum)cs.newInstance();//返回Object,必须进行转型,,创建的类必须具有默认构造器
System.out.println(list);
Class cs3=Gum.class;//不会自动地初始化该class对象
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
new Cookie();
}
}
class Initable{
static final int s1=47;
static final int s2=ClassInitialization.rand.nextInt(1000);
static {
System.out.println("Initable");
}
}
class Initable2{
static int s3=147;
static {
System.out.println("Initable2");
}
}
class Initable3{
static int s4=74;
static {
System.out.println("Initable3");
}
}
public class ClassInitialization {
public static Random rand=new Random(47);
public static void main(String[] args) throws ClassNotFoundException {
Class d=Initable.class;//不进行初始化
System.out.println("Initable "+Initable.s1);//因为有编译器常量,不必初始化就可以读取
System.out.println("Initable "+Initable.s2);//强制进行初始化
System.out.println("Initable2 "+Initable2.s3);//如果一个static域不是final,那么在对它进行访问时,要先进行链接(分配空间)和初始化(初始化存储空间)
Class Initable =Class.forName("reflectClass.Initable3");
System.out.println("Initable3 "+Initable3.s4);
}
}
Class intClass=int.class;
Class<Integer> g=int.class;
g=Integer.class;
intClass=double.class;
package reflectClass; import java.util.ArrayList; import java.util.List; import org.junit.Test; class CountedInteger { private static long counter = 1; private final long id = counter++; public CountedInteger(long counter) { this.counter = counter; } public CountedInteger() { } public String toString() { return Long.toString(id); } } public class FilledList<T> { private Class<T> type; public FilledList() { } public FilledList(Class<T> type) { this.type = type; } public List<T> create(int d) { List<T> result = new ArrayList<T>(); try { for (int i = 0; i < d; i++) { result.add(type.newInstance()); } } catch (Exception e) { throw new RuntimeException(); } return result; } public static void main(String[] args) throws InstantiationException, IllegalAccessException { FilledList<CountedInteger> f = new FilledList<CountedInteger>(CountedInteger.class);// CountedInteger必须有默认构造器,否则,将得到异常,编译器不会产生任何警告 System.out.println(f.create(15)); }
当你用泛型语法用于Class对象时,newinstance将返回该对象的确切类型,而不仅仅是之前的Object。但是这在某种程度是受限
Class<Number> d=Number.class;
Number n = d.newInstance();//会返回确切类型,不必进行类型转换
Class<? super Integer> n2=d.getSuperclass();
Object o=n2.newInstance();//返回Object
标签:必须 img href 获得 .class 有趣 array 高效 etc
原文地址:https://www.cnblogs.com/QianYue111/p/10357023.html