-
写出获取Class实例的三种常见的方式
Class aclass = String.class; //写死了 没有动态性 Class aclass2 = 类名.getClass(); Class aclass3 = Class.forName(String classPath)//体现反射的动态性
-
Class的理解
Class 实例对应着加载到内存中的一个运行时类
-
创建Class对应运行时类的对象的
通用方法
,代码实现。以及这样的操作,需要对应的运行时类构造器方面满足的要求
Object obj = clazz.newInstance(); 构造器应该满足:1.空参构造器 2.构造器权限足够
-
如何调用方法show()
Class clazz = <Class_Name>.class; //如类名是user Class clazz = User.class; User u = (User) clazz.newInstance(); Method show = clazz.getDeclaredMethod("show",String.class); //指明获取的方法的形参列表 show.setAccessible(true); show.invoke(u,"xxxx") //给方法形参赋值的实参
1.描述一下JVM加载class文件的原理机制?
答:JVM中类的装载是由ClassLoader和它的子类来实现的,Java ClassLoader 是一个重要的Java运行时系统组件。它负责在运行时查找和装入类文件的类。
1)创建Student类,类中有属性name和age并封装属性
2)重载Student的构造函数,一个是无参构造并,另一个是带两个参数的有参构造,要求在构造函数打印提示信息
3)创建带main函数的NewInstanceTest类,利用Class类得到Student对象
4)通过上述获取的Class对象分别调用Student有参函数和无参函数
/Student.java:
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
private void show(){
System.out.println("[Fucking u,student ] :" );
}
private String display(String nation){
System.out.println(" My nation:" + nation);
return nation;
}
}
///NewInstanceTest.java/
public class NewInstanceTest {
public static void main(String[] args) throws Exception {
Class<Student> studentClass = Student.class;
Student student = (Student) studentClass.newInstance();
//Call no-argument function
Method show = studentClass.getDeclaredMethod("show");
show.setAccessible(true);
Object returnVal = show.invoke(student);
System.out.println(returnVal); // [Fucking u,student ] : null
//Call argument function
Method display = studentClass.getDeclaredMethod("display",String.class);
display.setAccessible(true);
Object returnVal1 = display.invoke(student,"CHN");
System.out.println(returnVal1); // My nation:CHN CHN
}
}
1)创建Mytxt类,创建myCreate()方法完成创建文件D:\myhello.txt文件的功能。创建带main方法的NewInstanceTest类1,通过Class类获取Mytxt对象,调用myCreat()
///Mytxt.java/
public void myCreate() throws IOException {
File file = new File("./myhello.txt");
if (!file.exists()){
file.createNewFile();
System.out.println("Create File success!");
}
}
///NewInstanceTest1///
public class NewInstanceTest1 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<Mytxt> mytxtClass = Mytxt.class;
Mytxt mytxt = (Mytxt) mytxtClass.newInstance();
Method myCreate = mytxtClass.getDeclaredMethod("myCreate");
myCreate.setAccessible(true);
Object returnVal = myCreate.invoke(mytxt,null);
System.out.println(returnVal);
}
}
1)自定义一个有效的Annotation注释名为MyAnnotation,其中有属性myname创建Student类并重写toString(),toString()要求使用三自定义的MyAnnotation注释
2)创建TestGetAnno类,打印出Student类的toString方法的注释
/MyAnnotation/
Class studentClass = Student.class;
Method[] declaredMethod = studentClass.getDeclaredMethods();
for(Method m : declaredMethod){
Annotation[] annotations = m.getAnnotations();
for (Annotation a : annotations){
System.out.println(a);
}
}
/Student/
@MyAnnotation(myname = "toNY")
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/TestGetAnno
Class studentClass = Student.class;
Method[] declaredMethod = studentClass.getDeclaredMethods();
for(Method m : declaredMethod){
Annotation[] annotations = m.getAnnotations();
for (Annotation a : annotations){
System.out.println(a);
}
}
利用Class类的forName方法得到File类
在控制台打印File类的所有构造器
通过newInstance的方法创建File对象,并创建D:\mynew.txt文件
public void Test1() throws Exception{
//利用Class类的forName方法得到File类
Class aClass = Class.forName("java.io.File");
//在控制台打印File类的所有构造器
Constructor[] declaredConstructor = aClass.getDeclaredConstructors();
for(Constructor con : declaredConstructor) System.out.println(con);
//获取File的一个构造器
Constructor constructor = aClass.getConstructor(String.class);
//用上面得到构造器创建File对象
Object instance = constructor.newInstance("./mynew.txt");
//获取File的createNewFile方法
Method createNewFile = aClass.getMethod("createNewFile");
createNewFile.invoke(instance);
1.定义PrivateTest类,有私有name属性,并且属性值为hellokitty,只提供name的getName的公有方法
2.创建带有main方法ReflectTest的类,利用Class类得到私有的name属性
3.修改私有的name属性值,并调用getName()的方法打印name属性值
/PrivateTest
public class PrivateTest {
private String name = "FuckingWork";
public String getName() {
return name;
}
public PrivateTest() {
}
public PrivateTest(String name) {
this.name = name;
}
}
/ReflectTest
public class ReflectTest {
public static void main(String[] args) throws Exception {
Class privateTestClass = PrivateTest.class;
PrivateTest pt = (PrivateTest) privateTestClass.newInstance();
Field[] declaredFields = privateTestClass.getDeclaredFields();
for (Field f : declaredFields){
System.out.println(f);
String fName = f.getName();
System.out.print(fName);
}
System.out.println();
Method getName = privateTestClass.getDeclaredMethod("getName");
getName.setAccessible(true);
Object value = getName.invoke(pt);
System.out.println(value);
}
}