本帖最后由 垂天云 于 2013-9-17 20:51 编辑
- package cn.itcast.day01;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- public class ReflectTest {
- /**
- * @param args
- */
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- String str1 = "abc";
- Class cls1 = str1.getClass();
- Class cls2 = String.class;
- Class cls3 = Class.forName("java.lang.String");
- System.out.println(cls1 == cls2);
- System.out.println(cls1 == cls2);
- System.out.println(cls3 == cls2);
- System.out.println(cls1.isPrimitive());
- System.out.println(int.class.isPrimitive());
- System.out.println(int.class == Integer.class);
- System.out.println(int.class == Integer.TYPE);
- System.out.println(int[].class.isPrimitive());
- System.out.println(int[].class.isArray());
- //new String(new StringBuffer("abc"));
- Constructor constructor1 = String.class.getConstructor(StringBUffer.class);//该句报错???···肿么回事···求解决?
- String str2 = (String)constructor1.newInstance(new StringBuffer("abc"));
- System.out.println(str2.charAt(2));
- ReflectPoint pt1 = new ReflectPoint (3,5);
- Field fieldY = pt1.getClass().getField("y");
- fieldY.get(pt1);
- System.out.println(pt1);
- }
- }
复制代码- package cn.itcast.day01;
- public class ReflectPoint {
- private int x;
- public int y;
- public ReflectPoint(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- }
- }
复制代码 问题在注释中,报错了····不知道怎么解决·····
|