您现在的位置是:主页 > news > php网站建设工程师/站长工具使用方法

php网站建设工程师/站长工具使用方法

admin2025/6/2 0:59:12news

简介php网站建设工程师,站长工具使用方法,网站咨询弹窗是怎么做的,合肥网页设计兼职概念定义什么是反射?JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法,这种动态获取、调用对象方法的功能称为java语言的反射机…

php网站建设工程师,站长工具使用方法,网站咨询弹窗是怎么做的,合肥网页设计兼职概念定义什么是反射?JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法,这种动态获取、调用对象方法的功能称为java语言的反射机…

概念定义

什么是反射?JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法,这种动态获取、调用对象方法的功能称为java语言的反射机制。

反射是通过Class对象(字节码文件),来知道某个类的所有属性和方法。也就是说通过反射我们可以获取构造器,对象,属性,方法。

执行流程

318d7cd4a6fd4d3161a8339535936f02.png

获取Class对象的方式

Class student = Student.class; //方式一

Class> forName = Class.forName("com.test.utils.reflection.Student"); //方式二

通过Class对象获取类加载器

下面的这个类创建了多个构造方法

public class Student {

Student(String name) {

System.out.println("用default修饰的Student的含有一个String参数的构造器:"+name);

}

public Student() {

System.out.println("用public修饰的Student的无参构造器");

}

public Student(String name,int age) {

System.out.println("用public修饰的Student的含有两个参数的构造器:"+name+age);

}

public Student(boolean sex) {

System.out.println("用public修饰的Student的含有一个参数的构造器:"+sex);

}

protected Student(int age) {

System.out.println("用protected修饰的Student的含有一个参数的构造器:"+age);

}

private Student(String name,int age,boolean sex) {

System.out.println("用private修饰的Student的含有三个参数的构造器:"+name+age+sex);

}

private void test(String name) {

System.out.println("方法调用成功:"+name);

}

}

public static void main(String[] args) throws Exception {

// 生成对应的类

Class student = Student.class;

System.out.println("输出所有的public构造方法");

// 获取当前类的所有的public构造方法

Constructor>[] constructors = student.getConstructors();

for (Constructor constructor: constructors) {

System.out.println(constructor);

}

System.out.println("输出所有的构造方法");

// 获取当前类的所有的构造方法

Constructor>[] declaredConstructors = student.getDeclaredConstructors();

for (Constructor constructor: declaredConstructors) {

System.out.println(constructor);

}

// 调用这个构造方法 private Student(String name,int age,boolean sex)

Constructor constructor = student.getDeclaredConstructor(String.class,int.class,boolean.class);

// 该方法将忽略构造器的修饰符,即可在其他package下通过反射调用private的构造方法

constructor.setAccessible(true);

// 实例化一个对象

Student studentInstance = (Student) constructor.newInstance("11",2,true);

System.out.println("Student对象实例:" + studentInstance);

System.out.println("获取所有public的方法");

// 获取当前类的所有public的方法

Method[] declaredMethods = student.getDeclaredMethods();

for (Method method:declaredMethods) {

System.out.println(method);

}

System.out.println("获取所有的方法");

// 获取所有的方法

Method[] methods = student.getMethods();

for (Method method:methods) {

System.out.println(method);

}

}

执行结果

输出所有的public构造方法

public com.test.utils.reflection.Student(boolean)

public com.test.utils.reflection.Student()

public com.test.utils.reflection.Student(java.lang.String,int)

输出所有的构造方法

private com.test.utils.reflection.Student(java.lang.String,int,boolean)

protected com.test.utils.reflection.Student(int)

public com.test.utils.reflection.Student(boolean)

com.test.utils.reflection.Student(java.lang.String)

public com.test.utils.reflection.Student()

public com.test.utils.reflection.Student(java.lang.String,int)

用private修饰的Student的含有三个参数的构造器:112true

Student对象实例:com.test.utils.reflection.Student@2280cdac

获取所有public的方法

public static void com.test.utils.reflection.Student.main(java.lang.String[]) throws java.lang.Exception

private void com.test.utils.reflection.Student.test(java.lang.String)

获取所有的方法

public static void com.test.utils.reflection.Student.main(java.lang.String[]) throws java.lang.Exception

public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException

public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException

public final void java.lang.Object.wait() throws java.lang.InterruptedException

public boolean java.lang.Object.equals(java.lang.Object)

public java.lang.String java.lang.Object.toString()

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()