publicclassTest{ //使用forName方法会抛出Class不存在异常 publicstaticvoidmain(String[] args)throws ClassNotFoundException { Person p = new Person(); Class c = p.getClass(); System.out.println(c.getName()); String className = "java.util.Random"; Class c2 = Class.forName(className); System.out.println(c2.getName()); Class c3 = int.class; Class c4 = Random.class; Class c5 = double[].class; System.out.println(c3.getName()); System.out.println(c4.getName()); System.out.println(c5.getName()); } }
输出结果:
com.xzc.Person java.util.Random int java.util.Random [D
publicclassTest{ publicstaticvoidmain(String[] args){ //getConstructor与newInstance方法均会抛出异常 Person p = new Person(); Class c = p.getClass(); try { Object obj = c.getConstructor().newInstance(); //向下强制类型转换 ((Person)obj).setAge(10); ((Person)obj).setName("A person created by Class"); System.out.println(obj.toString()); }catch (Exception e){ e.printStackTrace(); } } }
publicclassTest{ publicstaticvoidmain(String[] args)throws NoSuchFieldException { Person p = new Person(); Class c = p.getClass(); Field f = c.getField("name");
[public java.lang.String com.xzc.Person.getName(), public java.lang.String com.xzc.Person.toString(), public void com.xzc.Person.setName(java.lang.String), public int com.xzc.Person.getAge(), public void com.xzc.Person.setAge(int), public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()]
[public java.lang.String com.xzc.Person.name]
[public java.lang.String com.xzc.Person.name, private int com.xzc.Person.age]
publicclassTest{ //get方法和getField方法会抛出异常 publicstaticvoidmain(String[] args)throws NoSuchFieldException, IllegalAccessException { Person p = new Person(); Class c = p.getClass(); p.setName("Hawkeye"); Field f = c.getField("name"); Object v = f.get(p); System.out.println(v); f.set(p,"HawkeyeNew"); v = f.get(p); System.out.println(v); } }
publicclassTest{ //get方法和getField方法会抛出异常 publicstaticvoidmain(String[] args)throws NoSuchFieldException, IllegalAccessException { Person p = new Person(); Class c = p.getClass(); p.setName("Hawkeye"); p.setAge(10); //注意这里age是private字段所以要用getDeclaredField Field f = c.getDeclaredField("age"); f.setAccessible(true); Object v = f.get(p); System.out.println(v); } }
public Object[] badCopyOf(Object[] a,int newLen){ var newArray = new Object[newLen]; System.arraycopy(a,0,newArray,0,Math.min(a.length,newLen)); return newArray; }
publicclassTest{ //getMethon方法和invoke方法会抛出异常 publicstaticvoidmain(String[] args)throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Person p = new Person(); p.setName("Hawkeye"); p.setAge(10); Class c = p.getClass(); Method m = c.getMethod("getName"); Method m2 = c.getMethod("getAge"); //返回Object类要转型 String n = (String) m.invoke(p); System.out.println(n); int a = (int) m2.invoke(p); int b = (Integer) m2.invoke(p); System.out.println(a); System.out.println(b); } }
publicclassTest{ //getMethon方法和invoke方法和getConstr方法会抛出异常 publicstaticvoidmain(String[] args)throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { Person p = new Person(); p.setName("Hawkeye"); p.setAge(10); Class c = p.getClass(); Method m = c.getMethod("getName"); Method m2 = c.getMethod("getAge"); //返回Object类要转型 String n = (String) m.invoke(p); System.out.println(n); int a = (int) m2.invoke(p); int b = (Integer) m2.invoke(p); System.out.println(a); System.out.println(b); Method m3 = c.getMethod("getAge",int.class);//重点 int d = (int)m3.invoke(p,10);//方法有一个int参数要赋予一个int值 System.out.println("Age after 10 years:"+d); //无参构造器前面已经说了我们直接上有参,无非就是传几个参数的class。 Constructor con = c.getConstructor(String.class,int.class);//重点 Object o = con.newInstance("HawkeyeNew",15);//构造器有参数了要赋予参数 System.out.println(o.toString()); } }
输出结果:
Hawkeye 10 10 Age after 10 years:20 Name: HawkeyeNew Age: 15