//运用.calss的方式获取Class实例(类)
Class c3=Point.calss;
System.out.println(c3.getName()); //结果:Point
//运用.calss的方式获取Class实例(基本类型)
Class c4=int.calss;
System.out.println(c4.getName()); //结果:int
//运用.calss的方式获取Class实例(基本数据类型的封装类)
Class c5=Integer.TYPE;
System.out.println(c5.getName()); //结果:int
Class c6=Integer.class;
System.out.println(c6.getName()); //结果:java.lang.Integer
*/
//以下结果是: before new Point()
loading point
after new Point()
loading Line
//当new Point()的时候加载这个类,用forName构造实例的时候也加载该类。
System.out.println("before new Point()");
new Point();
System.out.println("after new Point()");
Java code
import java.util.*;import java.lang.reflect.*;public class CSDN { public static void main(String args[]) throws Exception{ Set s = new HashSet(); s.add("foo"); Iterator it = s.iterator(); Class[] argsClass = new Class[0]; Method m = Iterator.class.getMethod("hasNext",argsClass); System.out.println(m.invoke(it,argsClass)); }}
可正常执行
Java code
import java.util.*;import java.lang.reflect.*;public class CSDN { public static void main(String args[]) throws Exception{ Set s = new HashSet(); s.add("foo"); Iterator it = s.iterator(); Class[] argsClass = new Class[0]; Method m = it.getClass().getMethod("hasNext",argsClass); System.out.println(m.invoke(it,argsClass)); }}