博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java反射讲解-实例(1)
阅读量:6585 次
发布时间:2019-06-24

本文共 4896 字,大约阅读时间需要 16 分钟。

hot3.png

首先定义一下user实体类

package me.explain.work.base.temp.entity;import java.io.Serializable;import java.util.Date;/** * Created by pro on 16-1-18. */public class User implements Serializable{    private long id;    private String name;    private String password;    private Date createtime;    private int sex;    private int status;    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public Date getCreatetime() {        return createtime;    }    public void setCreatetime(Date createtime) {        this.createtime = createtime;    }    public int getSex() {        return sex;    }    public void setSex(int sex) {        this.sex = sex;    }    public int getStatus() {        return status;    }    public void setStatus(int status) {        this.status = status;    }}

下面定义一个main方法类 查看一下这个user下面有的东西

试一下啊。

package me.explain.work.base.clazz;import me.explain.work.base.temp.entity.User;import java.lang.reflect.Field;import java.lang.reflect.Method;/** * Created by pro on 16/1/20. */public class MainClazz {    public static void main(String[] args) throws Exception{        /** 定义一个实体类 **/        User user = new User();        Method[] methods = user.getClass().getDeclaredMethods();        for(Method method : methods) {            System.out.println(method.getName());        }        Field[] fields = user.getClass().getDeclaredFields();        for(Field field : fields) {            System.out.println(field.getName());        }    }}

上面的方法 获取了该类里面的字段和方法

下面看下运行结果。

/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin/java -Didea.launcher.port=7536 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 15.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/tools.jar:/explain/work/idea15/explain.parent/work.base/target/classes:/Users/pro/.m2/repository/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar:/Applications/IntelliJ IDEA 15.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain me.explain.work.base.clazz.MainClazz

getName

setName

getId

setId

setSex

getCreatetime

setCreatetime

setPassword

getSex

getStatus

setStatus

getPassword

Process finished with exit code 0

好了,字段和方法都获取到了,下面还有什么不了解的,可以随时在下面评论。

转载于:https://my.oschina.net/superman158/blog/606990

你可能感兴趣的文章
htons函数详解
查看>>
php获取本地ip-针对linux
查看>>
Python基于http的ddos攻击代码
查看>>
Netbeans java 远程调试
查看>>
深度学习
查看>>
难以执行,让你不相信自己的眼睛的SQL
查看>>
使用github管理iOS分布式项目开发
查看>>
Spark中文python文档
查看>>
Flip Grid View
查看>>
CK Calendar for iOS
查看>>
Popover View in iPhone
查看>>
D3Kit
查看>>
JSLockScreen
查看>>
判断ios中是否安装了某些软件以及那些软件处于运行等待状态
查看>>
UML建模之活动图介绍(Activity Diagram)
查看>>
开源 免费 java CMS - FreeCMS1.9 简历管理
查看>>
Beyond Compare 4提示已经过了30天试用期
查看>>
android8.0之SystemUI分析(一)
查看>>
OpenCV中路径分割符的使用
查看>>
Java_Ant详解
查看>>