Java?з???
???????????? ???????[ 2017/3/1 10:39:27 ] ??????????????????? Java
????????
??????????????????java????Java?У???????????????????????????????????????????????????????????????????????????????????????????????????????????????в???????????????????????????????????У???????洢??????????????????????????????????????????????????????????????е?????????????????????????????????????????????Щ???????????????????????Class???????
?????????????Java???е????????????????????????????????????????????Java??Class????????????????е?Java??ó????е????????????????????????????????????????????Class?????????????о??????????????γ???????鶼?????Class??????????Java?????boolean??byte??char??short??int??long??float??double????????void?????Class????Class??й???????????Class ???????????????? Java ???????????????????????е? defineClass ?????????????
??????????????????
??????λ?????????????????????????
??????????.class??????System.class
?????????.getClass()??????new Date().getClass();
??????Class.forName("????")??????Class.forName("java.lang.Object");--?????????????
????1?????????????е??????
????Constructor[] constructors = Class.forName(“java.lang.String”).getConstructors();
????2??????????????????????????????????
????Constructor constructor = Class.forName(“java.lang.String”)
????.getConstructor(String.class); //??÷?????????????
????3?????????????
????????????String str = new String(“abc”);
?????????????????
????Constructor cons = Class.forName(“java.lang.String”)
????.getConsturctor(String.class);
????String str = (String)cons.newsInstance(“abc”);
????????????????????
????Field???????????е???????????
????Field fieldy = rp1.getClass().getField(“y”);
????System.out.println(“fieldy = “+ fieldy.getInt(rp1));
????//???????????????б????????
????Field fieldx = rp1.getClass().getDeclaredField(“x”); //?????е????
????fieldx.setAccessible(true); //??????????
????System.out.println(“fieldx = “+ fieldx.getInt(rp1));
????>
????/**
????* ???????????????????г??????????????????
????* @param obj
????* @throws Exception
????*/
????private static void changeStringValue(Object obj) throws Exception {
????Field[] fields = obj.getClass().getDeclaredFields();
????for(Field field : fields){
????field.setAccessible(true);
????if(field.getType() == String.class){
????String oldValue = (String) field.get(obj);
????String newValue = oldValue.replace(‘b’??’a’);
????field.set(obj??newValue);
????}
????}
????}
?????????????????
????Method charAt = String.class.getMethod("charAt"?? int.class);
????// char invoke = (char) charAt.invoke(str1?? 1);
????char invoke = (char) charAt.invoke(str1?? new Object[]{2}); charAt.invoke(null?? 1); //?????????????null????ζ?????????????????
????TestArguments.main(new String[]{"1233"??"3454"??"345"});
????//???÷???????????????е?main????
????String startClassName = "com.biaoyoo.reflect.TestArguments";
????Method mainMethod = Class.forName(startClassName)
????.getMethod("main"?? String[].class);
????//mainMethod.invoke(null??(Object) new String[]{"1233"??"3454"??"345"}); //??????????
????mainMethod.invoke(null??new Object[]{ new String[]{"1233"??"3454"??"345"}}); //??????????????
????class TestArguments{
????public static void main(String[] args){
????for(String arg: args){
????System.out.println(arg);
????}
????}
????}
???????????????
????/**
????* ?????????
????* @param obj
????*/
????private static void printObject(Object obj){
????Class clazz = obj.getClass();
????if(clazz.isArray()){ //??????
????int length = Array.getLength(obj);
????for(int i=0;i<length;i++){
????System.out.println(Array.get(obj??i));
????}
????}else{
????System.out.println(obj);
????}
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11