类是程序的一部分,每个类都有一个Class对象。换言之,每当编写并且编译了一个新类,就会产生一个Class对象(更恰当的说,是被保存在一个同名的.class文件中)
那么,我们势必要掌握好Class类常用方法的使用,这在今后的编程中是十分重要的。
以下归纳主要参考资料:《THINKING IN JAVA》第四版 以及 网上资料
常用方法:
- Class.forName("ClassName")返回类名所需的类,这里的ClassName必须包括包名;
- Class.getName()返回全限定的类名;
- Class.CanonicalName()返回不包含包名的全限定名;
- Class.getSimpleName()返回不包含包名的类名;
- Class.getInterface()返回Class对象,表示在感兴趣的Class对象中所包含的接口;
- Class.getSuperclass()返回所选类基类;
- Class.newInstance()实现“虚拟构造器”的一种途径,虚拟构造器允许你声明:“我不知道你的确切类型,但是无论如何要正确的创建你自己。”在如下的实例中,up仅仅只是一个Class引用,在编译期间不具备任何更进一步的类型信息。当你创建实例时,会得到Object引用,但是这个引用指向的是Toy对象。另外使用newInstance()来创建类,必须带有默认的构造器。
<span style="font-size:24px;"><span style="font-size:24px;"><pre name="code" class="java">package typeinfo.toys; import typeinfo.toys.HasBatteries; import typeinfo.toys.Shoots; import typeinfo.toys.Toy; import typeinfo.toys.Waterproof; //import typeinfo.toys.*; interface HasBatteries{} //接口位置 interface Waterproof{} interface Shoots{} class Toy{ Toy(){} Toy(int i){} } class FancyToy extends Toy implements HasBatteries,Waterproof,Shoots{ FancyToy(){super(1);} //super 调用父类构造函数 } public class ToyTest{ static void printfInfo(Class cc){ System.out.print("class name:\"" + cc.getName() + "\" is interface?"); System.out.println("[" + cc.isInterface() + "]"); System.out.println("SimpleName:\"" + cc.getSimpleName() + "\""); System.out.println("CanonicalName:\"" + cc.getCanonicalName() + "\""); } public static void main(String[] args){ Class c = null; try{ //Class c = Class.forName("typeinfo.toys.FancyToy"); 这样c变成了局部变量在域{}外失效 c = Class.forName("typeinfo.toys.FancyToy"); }catch(ClassNotFoundException e){ System.out.println("ClassNotFound!"); System.exit(1); } printfInfo(c); for(Class face : c.getInterfaces())printfInfo(face); Class up = c.getSuperclass(); Object obj = null; try{ obj = up.newInstance(); //up.newInstance(); }catch(InstantiationException e){ System.out.println("Cannot instanciate!"); System.exit(1); } catch(IllegalAccessException e){ System.out.println("Cannot access!"); System.exit(1); } printfInfo(obj.getClass()); } }</span></span>
输出:
class name:"typeinfo.toys.FancyToy" is interface?[false]
SimpleName:"FancyToy"
CanonicalName:"typeinfo.toys.FancyToy"
class name:"typeinfo.toys.HasBatteries" is interface?[true]
SimpleName:"HasBatteries"
CanonicalName:"typeinfo.toys.HasBatteries"
class name:"typeinfo.toys.Waterproof" is interface?[true]
SimpleName:"Waterproof"
CanonicalName:"typeinfo.toys.Waterproof"
class name:"typeinfo.toys.Shoots" is interface?[true]
SimpleName:"Shoots"
CanonicalName:"typeinfo.toys.Shoots"
class name:"typeinfo.toys.Toy" is interface?[false]
SimpleName:"Toy"
CanonicalName:"typeinfo.toys.Toy"
拓展:关于getName、getCanonicalName 的区分
在上例中,我们可以看到getName和getCanonicalName之间有很多的相同、不同。
总的来说:
其实getName、getCanonicalNam这两个方法没有什么不同的,对于大部分class来说,但是对于array或内部类等就显示出来了。
getName返回的是[[Ljava.lang.String之类的表现形式,而getCanonicalName返回的就是跟我们声明类似的形式。
BTW,在load class的时候需要的名字也是getName这种的名字
在根据类名字创建文件的时候最好使用getCanonicalName()