package com.reflect;
public class TestClass
{
public static void main(String[] args)
{
try
{
Class c=Class.forName("com.entity.Student");
Object obj=c.newInstance();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (InstantiationException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}
package com.entity;
public class Student
{
private String name;
private int age;
private int money;
private String address;
private Course course;
public Student()
{
super();
System.out.println("student");
}
public Student(String name, int age)
{
super();
this.name = name;
this.age = age;
System.out.println(this.name+","+this.age);
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
private String getHello()
{
return "hello";
}
}
Java反射实战:创建和实例化com.entity.Student对象
本文通过Java反射API展示了如何动态加载com.entity.Student类并实例化对象,包括处理ClassNotFoundException、InstantiationException和IllegalAccessException。重点在于理解类加载和对象创建过程。
335

被折叠的 条评论
为什么被折叠?



