java使用反射技术示例
复制代码 代码如下:
package ;
import tructor;
import d;
import cationTargetException;
import od;
import yList;
import ys;
import ator;
import ;
import ;
import Shelf;
import etaDataCloumName;
public class GetNewInstances
Class[] cl = {};
Object[] ob = {};
/**
* 每次用完之后设为空 不然会累加
*/
public void setNullToArrays(){
= new Class[]{};
= new Object[]{};
}
/**
* copy Object数组
*
* @param obj
* 构造方法里需要的实际值
* @return
*/
public Object[] getObjectArrays(Object obj) {
ob = Of(ob,th + 1);
ob[th - 1] = obj;
return ob;
}
/**
* copy Class 数组
*
* @param cla
* 要添加的class
*
* @return
*/
@SuppressWarnings("unchecked")
public Class[] getClassArrays(Class cla) {
if (cla != null) {
cl = Of(cl,th + 1);
cl[th - 1] = cla;
return cl;
}else{
return cl;
}
}
/**
* 得到类的实例
*
* @param clazz
* 要实例化的类
* @return 实例化之后的'类
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
@SuppressWarnings("unchecked")
public Object getClassNewInstance(Class clazz)
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, SecurityException,
InvocationTargetException, NoSuchMethodException {
Object oj = null;
Constructor[] cons = eclaredConstructors();// 得到构造函数
Class[] cla = cons[1]arameterTypes();
tln("提示用户是否需要添加字段 构造函数参数的大小:"+th);
for (int i = 0; i < th; i++) {
String classStr = cla[i]ring();
// tln("参数的类型:"+classStr);
if (ls("class ng")) {
getClassArrays(s);
} else if (ls("int")) {
getClassArrays(s);
} else if (ls("double")) {
getClassArrays(s);
} else if (ls("boolean")) {
getClassArrays(s);
} else if (ls("float")) {
getClassArrays(s);
} else if (ls("class ger")) {
getClassArrays(s);
}else if(ls("class t")){
getClassArrays(s);
}
}
oj = nstance();//返回当前对象 具体的实例化构造在BDOperater
return oj;
}
/**
* 通过构造函数得到具体的实例类
* @param clazz
* @return
* @throws IllegalArgumentException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public Object getObjCon(Class clazz) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
Object obj=null;
obj = lassNewInstance(clazz);
return obj;
}
/**
* 得到对象的实例
* @param clazz
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public Object getNewinstance(Class clazz) throws InstantiationException, IllegalAccessException{
Object obj = null;
obj = nstance();
return obj;
}
/**
* 根据反射得到类中的所有属性
* @param clazz 需要被获取属性的类
* @return 属性集合
* @throws SecurityException
* @throws IllegalArgumentException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public Field[] getFielsdArray(Class
Field[] fields = null;
fields = eclaredFields();
return fields;
}
/**
* 根据字符串得到setter格式的属性
* @param str 需要格式化的属性
* @return
*/
public String getSetterStr(String str){
String info = null;
String strValue = tring(0,1)perCase();
info = "set"+strValue+tring(1,th());
return info;
}
/**
* 把setXX还原为XX
* @param str
* @return
*/
public String setSetStr(String str){
String info = null;
String strValue = tring(3,th());
String lower = tring(0)werCase()tring(0,1);
info = lower+tring(4,th());
return info;
}
/**
* 得到类中的方法
* @param clazz 需要的得到方法的类
* @return
*/
public Method[] getMethodsArray(Class clazz){
Method[] methods = ethods();
return methods;
}
/**
* 根据list
* @param listMap
* @param clazz
* @param tableName 数据库名称
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings({ "unchecked" })
public List
List
List
Iterator<Map
while(ext()){
Map
Iterator
while(ext()){
String info = ();
bjectArrays((info));
}
tln("调用反射:"+th+" "+th);
Object Tobj = lassNewInstance(clazz)lass()onstructor()nstance();
(Tobj);
ullToArrays();
}
return listLast;
}
public static void main(String[] args) {
GetNewInstances ge = new GetNewInstances();
tln(etterStr("nameSpace")=="setNameSpace");
tln("1a"=="1a");
tln(etStr("setNameSpace"));
}
}
-
2017年3月计算机二级考试Java关键字
大家回忆一下我们在学习汉语的时候,开始学的是什么?肯定是先学一些单个的字,只有认识了单个的字,然后才能组成词,然后才能慢慢的到句子,然后到文章。学习同计算机语言跟这个过程是一样的,首先我们得学习一些计算机看得懂的单个的字,那么这些单个字在Java里面就是关键...
-
java system类使用方法示例
常用的方法:复制代码代码如下:longcurrentTimeMillis();获取当前时间的毫秒值voidexit();终止当前正在运行的Java虚拟机。复制代码代码如下:publicstaticvoidMethod(){longl=entTimeMillis();tln(l);();}描述系统属性信息:Propertiesroperties();该方法获取的`信...
-
java的String=a; a==null和a.equals(null)这两个判断有什么区别
1、String的==与equal()在对字符串的相等判断,==判断的是地址是否相同,equal()判断的是字符值是否相同。大多数时候==跟equal()的结果都是相同的。这是因为String对象是不变模式的,如果你不是明确地new一个String对象,Java对于String对象的保存默认的是会把新生成...
-
java设计模式:工厂模式
导语:Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。下面我们来看看工厂模式,希望对大家有所帮助。工厂设计模式★场景和问题面向接口编程是java编程中的大家所...