反射总结2
2012-03-01 16:08 党飞 阅读(197) 评论(0) 收藏 举报---------------------- android培训、java培训、期待与您交流! ----------------------
说明:反射机制的应用
Camera接口:定义了takePhoto()方法。
Camera01类:一种照相机的类型,实现Camera接口。
Camera02类:另一种照相机的类型,实现Camera接口。
Seller类:卖照相机。
Customer类:买相机,有main方法(测试类)
1.Camera接口
package com.zlc.reflect;
publicinterfaceCamera{
//声明照相机必须可以拍照
publicvoid takePhoto();
}
2.Camera01类
package com.zlc.reflect;
publicclassCamera01implementsCamera{
privatefinalint prefixs =300;//300万象素
privatefinaldouble optionZoom=3.5;//3.5倍变焦
publicdouble weight =1.2;//重量(单位kg)
publicdouble size =0.1;//尺寸(单位m)
@Override
publicvoid takePhoto(){
System.out.println("Camera01 has taken a photo");
}
}
3.Camera02类
package com.zlc.reflect;
publicclassCamera02implementsCamera{
privatefinalint prefixs =400;
privatefinaldouble optionZoom=5;
@Override
publicvoid takePhoto(){
System.out.println("Camera02 has taken a photo");
}
}
4.Seller类
package com.zlc.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
//Seller类通过Java反射机制实现
publicclassSeller{
//向顾客描述商品信息
publicvoid getDescription(String type){
try{
Class cls =Class.forName(type);
//生成一个实例对象,在编译时我们并不知道obj是什么类型。
Object obj = cls.newInstance();
//获得type类型所有已定义类变量及方法。
Field[] fs = cls.getFields();//获得本类及父类中声明为public的公共字段
Field[] fileds = cls.getDeclaredFields();//获得本类中已经声明的字段(包含public,private,默认的)
Method[] methods = cls.getDeclaredMethods();//获得本类中已经声明的方法(包含public,private,默认的)
System.out.println("The arguments of this Camera is:");//相机类中有哪些字段
for(int i=0;i<fileds.length;i++){
fileds[i].setAccessible(true);
//值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查
//输出类变量的定义及obj实例中对应的值
System.out.println(fileds[i].getName()+"-->"+fileds[i].get(obj));//get(obj)返回指定对象上此 Field 表示的字段的值
}
System.out.println("-------------------------------------");
System.out.println("获取指定类及父类中的公有字段:");
for(int i=0;i<fs.length;i++){
System.out.println(fileds[i].getName()+"-->"+fileds[i].get(obj));
}
System.out.println("-------------------------------------");
System.out.println("The function of this Camera:");//相机拍照的功能
for(int i=0;i<methods.length;i++){
//输出类中方法的定义
System.out.println(methods[i]);
}
}catch(Exception e){
System.out.println("Sorry , no such type");
}
}
//使用商品的某个功能
publicvoid testFuction(Object obj,Stringfunction){
try{
Class cls = obj.getClass();
//获得cls类中定义的无参方法。
Method m = cls.getMethod(function,null);
//调用obj中名为function的无参方法。
m.invoke(obj,null);
}catch(Exception e){
System.out.println("Sorry , no such function");
}
}
//拿商品给顾客
publicObject getCamera(String type){
try{
Class cls =Class.forName(type);
Object obj = cls.newInstance();
return obj;
}catch(Exception e){
System.out.println("Sorry , no such type");
returnnull;
}
}
}
5.Customer类(测试类)
package com.zlc.reflect;
publicclassCustomer{
publicstaticvoid main(String[] args){
//找到一个售货员
Seller seller =newSeller();
//向售货员询问两种相机的信息
seller.getDescription("com.zlc.reflect.Camera01");
seller.getDescription("com.zlc.reflect.Camera02");
//觉得Camera02比较好,叫售货员拿来看
Camera camera =(Camera)seller.getCamera("com.zlc.reflect.Camera02");
//让售货员拍张照试一下
seller.testFuction(camera,"takePhoto");
}
}
打印结果:
The arguments of this Camera is:
prefixs-->300
optionZoom-->3.5
weight-->1.2
size-->0.1
-------------------------------------
获取指定类及父类中的公有字段:
prefixs-->300
optionZoom-->3.5
-------------------------------------
The function of this Camera:
public void com.zlc.reflect.Camera01.takePhoto()
The arguments of this Camera is:
prefixs-->400
optionZoom-->5.0
-------------------------------------
获取指定类及父类中的公有字段:
-------------------------------------
The function of this Camera:
public void com.zlc.reflect.Camera02.takePhoto()
Camera02 has taken a photo
---------------------- android培训、java培训、期待与您交流! ----------------------
浙公网安备 33010602011771号