代码如下:
package ClassObjectPack;public class Student {private String name;int age;public String address;public Student(String name, int age, String address) {this.name = name;this.age = age;this.address = address;}public Student() {}private Student(String name) {this.name = name;}Student(String name, int age) {this.name = name;this.age = age;}private void function(){System.out.println("function");}public void method1(){System.out.println("method");}public void method2(String s){System.out.println("method:"+s);}public String method3(String s,int i){return s+","+i;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", address='" + address + '\'' +'}';}
}
package ClassObjectPack;public class ReflectDemo {public static void main(String[] args) throws ClassNotFoundException {//使用类的class属性来获取该类对应的Class对象Class<Student> c1 = Student.class;System.out.println(c1);Class<Student> c2 = Student.class;System.out.println(c2==c1);System.out.println("--------------------------");//调用对象的getClass()方法,返回该对象所属类对应的Class对象Student s = new Student();Class<? extends Student> c3 = s.getClass();System.out.println(c1==c3);//使用Class类中的静态方法forName(String className)Class<?> c4 = Class.forName("ClassObjectPack.Student");System.out.println(c1==c4);}
}