java引用class_【ThinkingInJava】27、关于class对象引用的各种关于class的方法

/**

* 书本:《Thinking In Java》

* 功能:关于class对象引用的各种关于class的方法

* 文件:ToyTest.java

* 时间:2015年4月12日19:21:32

* 作者:cutter_point

*/

package Lesson14TypeInformation.toys;

import static net.mindview.util.Print.*;

interface HasBatteries {}

interface Waterproof {}

interface Shoots {}

class Toy

{

Toy() {}

Toy(int i) {}

}

class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots

{

FancyToy() { super(1); }

}

class A

{

public String aa = " wuwuwwu";

public A() {System.out.println("========================");}

public A(String a) { this.aa = a; }

public void get()

{

System.out.println("=====" + aa);

}

}

public class ToyTest

{

static void printInfo(Class cc)

{

print("Class name: " + cc.getName() + " is interface? [" + cc.isInterface() +"]");

print("Simple name: " + cc.getSimpleName());

print("Canonical name : " + cc.getCanonicalName());//经典名,就是全名包括包

}

public static void main(String[] args)

{

Class c = null;

try

{

c = Class.forName("Lesson14TypeInformation.toys.FancyToy");

}

catch (ClassNotFoundException e)

{

print("Cant`t find FancyToy");

System.exit(1);

}

printInfo(c);

for(Class face : c.getInterfaces())

{

printInfo(face);//输出这个类型的每一个接口

}

Class up = c.getSuperclass();//得到父类

Object obj = null;

try

{

obj = up.newInstance();

}

catch (InstantiationException e)

{

print("Cannot instantiate");

System.exit(1);

}

catch (IllegalAccessException e)

{

print("Cannot access");

System.exit(1);

}//实例化一个对象

printInfo(obj.getClass());

Class b = null;

A a1 = new A("a111111111111111111111");

//A a2 = new A("a222222222222222222222");

Object a3 = null;

A a4 = null;

try {

b = Class.forName("Lesson14TypeInformation.toys.A");

//Class th = b.getClass();

a4= (A) b.newInstance();//得到类的对象,但是没有实例化

//a4 = (A) a3;

a4.get();

} catch (Exception e)

{

e.printStackTrace();

}

}

}

输出:

Class name: Lesson14TypeInformation.toys.FancyToy is interface? [false]  obj1

Simple name: FancyToy  obj1

Canonical name : Lesson14TypeInformation.toys.FancyToy  obj1

Class name: Lesson14TypeInformation.toys.HasBatteries is interface? [true]  obj1

Simple name: HasBatteries  obj1

Canonical name : Lesson14TypeInformation.toys.HasBatteries  obj1

Class name: Lesson14TypeInformation.toys.Waterproof is interface? [true]  obj1

Simple name: Waterproof  obj1

Canonical name : Lesson14TypeInformation.toys.Waterproof  obj1

Class name: Lesson14TypeInformation.toys.Shoots is interface? [true]  obj1

Simple name: Shoots  obj1

Canonical name : Lesson14TypeInformation.toys.Shoots  obj1

Class name: Lesson14TypeInformation.toys.Toy is interface? [false]  obj1

Simple name: Toy  obj1

Canonical name : Lesson14TypeInformation.toys.Toy  obj1

========================

===== wuwuwwu

原文:http://blog.csdn.net/cutter_point/article/details/45786133

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值