Java.反射

server/2024/12/23 5:45:30/

目录

1.获取class 的三种方式

2.利用反射获取构造方法

3.利用反射获取成员变量

4.利用反射获取成员方法


1.获取class 的三种方式

全类名怎么找?

全类名=报名+类名

java">package MyReflect;public class Student {private String id;private String name;private int age;private String address;public Student(String id, String name, int age, String address) {this.id = id;this.name = name;this.age = age;this.address = address;}public Student() {}/*** 获取* @return id*/public String getId() {return id;}/*** 设置* @param id*/public void setId(String id) {this.id = id;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}/*** 获取* @return address*/public String getAddress() {return address;}/*** 设置* @param address*/public void setAddress(String address) {this.address = address;}public String toString() {return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}";}public String toMess(){return id+"    "+name+"    "+age+"    "+address;}
}

java">package MyReflect;public class Test {public static void main(String[] args) throws ClassNotFoundException {//1  常用Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);//2   当作参数Class clazz2=Student.class;System.out.println(clazz2);System.out.println(clazz1 == clazz2);//3   当有对象的时候Student s1=new Student();Class clazz3=s1.getClass();System.out.println(clazz3);System.out.println(clazz1 == clazz3);}}

2.利用反射获取构造方法

java">package MyReflect;public class Student{private String name;private int age;public Student() {}protected Student(String name) {this.name = name;}protected Student(int age) {this.age = age;}private  Student(String name, int age) {this.name = name;this.age = age;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student111{name = " + name + ", age = " + age + "}";}}
java">package MyReflect;import java.lang.reflect.Constructor;public class Test {public static void main(String[] args) throws ClassNotFoundException {//1.获取字节码Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);//2.获取构造方法Constructor[] constructor1= clazz1.getConstructors();for (Constructor constructor:constructor1) {System.out.println(constructor);}}}

java">package MyReflect;import java.lang.reflect.Constructor;public class Test {public static void main(String[] args) throws ClassNotFoundException {//1  常用Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);Constructor[]constructor1= clazz1.getDeclaredConstructors();for (Constructor constructor:constructor1) {System.out.println(constructor);}}}

java">package MyReflect;import java.lang.reflect.Constructor;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {//1  常用Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);Constructor constructor1= clazz1.getDeclaredConstructor(String.class);System.out.println(constructor1);}}

3.利用反射获取成员变量

java">package MyReflect;import java.lang.reflect.Field;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Field[] fields=clazz.getDeclaredFields();for (Field field : fields) {System.out.println(field);}}}

 

java">package MyReflect;import java.lang.reflect.Field;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Field fields=clazz.getDeclaredField("name");//fields.setAccessible(true);System.out.println(fields);}}

对于private的变量,要

namefield.setAccessible(true);
java">package MyReflect;public class Student{private String name;private int age;public Student() {}protected Student(String name) {this.name = name;}protected Student(int age) {this.age = age;}public  Student(String name, int age) {this.name = name;this.age = age;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student111{name = " + name + ", age = " + age + "}";}}
java">package MyReflect;import java.lang.reflect.Field;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Field namefield=clazz.getDeclaredField("name");System.out.println(namefield);Student student=new Student("leo",11);// namefield.setAccessible(true);Object value=namefield.get(student);System.out.println("name:"+value);}}

4.利用反射获取成员方法

java">package MyReflect;import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method[] methods=clazz.getDeclaredMethods();for (Method method : methods) {System.out.println(method);}}}

java">package MyReflect;import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method[] methods=clazz.getMethods();for (Method method : methods) {System.out.println(method);}}}

java">package MyReflect;import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method method=clazz.getMethod("getName");System.out.println(method);}}

java">package MyReflect;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {Student student = new Student("leo",11);//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method method=clazz.getDeclaredMethod("eat", String.class);System.out.println(method);method.invoke(student,"汉堡包");}}

 

java">package MyReflect;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {Student student = new Student("leo",11);//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method method=clazz.getDeclaredMethod("eat", String.class);method.setAccessible(true);System.out.println(method);method.invoke(student,"汉堡包");}}

 


http://www.ppmy.cn/server/125975.html

相关文章

使用FFmpeg压缩MP3格式音频

FFmpeg简介 FFmpeg 是一个开源的多媒体框架&#xff0c;能够录制、转换数字音频和视频&#xff0c;并将其转码到流行的格式。它被广泛应用于音视频处理领域&#xff0c;支持几乎所有的音视频格式和编解码器。以下是 FFmpeg 的一些关键特点和功能&#xff1a; 主要特点 跨平台…

Nginx的核心架构和设计原理

Nginx 是一个免费的、开源的、高性能 Http 服务器和反向代理。Nginx 的架构设计是为了提供高性能、稳定性和可扩展性。 Nginx 的主要架构组件和工作原理&#xff1a; 1、Master 进程&#xff1a;Nginx 的运行始于一个 master 进程&#xff0c;它负责管理所有的工作进程。mast…

【TypeScript学习】TypeScript基础学习总结二

主要记录ts中的类、接口与泛型 1.类 无论是在哪种语言中&#xff0c;类都是面向对象编程(OOP)的一个主要实现方式。能够实现代码更加灵活&#xff0c;更具有结构化。类作用都是提供一个模板&#xff0c;通过类可以创建多个具有相同结构的对象。 // 类的定义&#xff0c;与对象…

kubernetes的网络

kubernetes网络模型 Kubernetes采用的是基于扁平地址空间的网络模型&#xff0c;集群中的每个Pod都有自己的IP地址&#xff0c;Pod之间不需要配置NAT就能直接通信&#xff0c;同一个Pod中的容器共享Pod的IP&#xff0c;能够通过localhost通信Pod容器内的通信 当Pod被调度到某个…

计算机网络:计算机网络概述 —— 初识计算机网络

文章目录 计算机网络组成部分网络架构协议与标准网络设备网络类型作用实际应用案例 计算机网络 计算机网络是指将多台计算机通过通信设备和通信链路连接起来&#xff0c;以实现数据和信息的交换和共享的技术和系统。它是现代信息社会的基础设施之一&#xff0c;也是互联网的基…

【数据结构】堆(Heap)详解

在深入了解堆这一重要的数据结构之前&#xff0c;不妨先回顾一下我之前的作品 ——“二叉树详解”。 上篇文章&#x1f449;剖析二叉树&#xff08;Binary Tree&#xff09; 二叉树作为一种基础的数据结构&#xff0c;为我们理解堆以及其他更复杂的数据结构奠定了坚实的基础。它…

Leetcode 环形链表

java 代码&#xff1a; /*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val x;* next null;* }* }*/ public class Solution {public boolean hasCycle(ListNode head) {//初始…

使用 Nginx 和 Gunicorn 部署 Flask 项目详细教程

使用 Nginx 和 Gunicorn 部署 Flask 项目详细教程 在这篇文章中&#xff0c;我们将介绍如何使用 Nginx 和 Gunicorn 来部署一个 Flask 项目。这种部署方式非常适合在生产环境中使用&#xff0c;因为它能够提供更好的性能和更高的稳定性。 目录 Flask 项目简介环境准备Gunico…