本帖最后由 胡高坡 于 2013-1-5 11:17 编辑
package cn.itcast.day1;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.omg.CORBA.Object;
public class ReflectTest {
public static void main(String[] args) throws Exception {
ReflctPoint pt1=new ReflctPoint(3,5);
//把字符串中的a改写成b
chageStringValue( pt1);
System.out.println(pt1);
//TestArguments.main(new String[]{"111","222","333"});
String startingClassName=args[0];
Method mainMethod =Class.forName(startingClassName).getMethod("main", String[].class);
mainMethod.invoke(null,(Object)new String[]{"111","222","333"});
int[] a1=new int[3];
int[] a2=new int[4];
int[][] a3=new int[2][3];
String[] a4=new String[3];
System.out.println(a1.getClass()==a2.getClass());
Object o1=a1;
Object o2=a4;
}
private static void chageStringValue(Object obj) throws Exception {
// TODO Auto-generated method stub
Field[] fields=obj.getClass().getFields();
for(Field field:fields){
if(field.getType()==String.class){
String oldvalue=(String) field.get(obj);
String newvalue=oldvalue.replace("a", "b");
field.set(obj, newvalue);
}
}
}
}
中chageStringValue( pt1);错误是The method chageStringValue(Object) in the type ReflectTest is not applicable for the arguments (ReflctPoint)。
mainMethod.invoke(null,(Object)new String[]{"111","222","333"});错误是Cannot cast from String[] to Object
Object[] o1=a1;错误是Type mismatch: cannot convert from int[] to Object
Object o2=a4;错误是Type mismatch: cannot convert from String[] to Object
这些是什么原因,我用的是myeclipse8.5,好像其他类型转Object都是错误的,怎么解决?
|