Class类常用方法(Java)

类是程序的一部分,每个类都有一个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()




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值