1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java基于反射得到对象属性值的示例代码分享

java基于反射得到对象属性值的示例代码分享

时间:2022-01-27 10:48:42

相关推荐

java基于反射得到对象属性值的示例代码分享

Java|java教程

java,反射,对象属性值

Java-java教程通过反射机制得到对象中的属性和属性值 在对象中private没问题,在别的类中有时会报异常。下面的例子是在本对象中

绿麻雀网贷系统源码,vscode加载的文件不同步,yoga ubuntu,tomcat默认安装在c,sqlite 判断索引,进入网页设计,福州公司变更服务器,douphp 插件免费下载,web前端运行框架,爬虫什么样,php做接口,seo网站培训班,类似淘宝网站模板,网页特效侧边,安装便利100模板,js 页面弹窗,php 手机内容管理系统,易语言修改程序命令怎么用lzw

/*** Engine entity. @author MyEclipse Persistence Tools*/public class Engine implements java.io.Serializable { // Fields private Long engineId; private String engineName; private String engineType; // Constructors /** default constructor */ public Engine() { } /** minimal constructor */ public Engine(Long engineId) { this.engineId = engineId; } public Engine(String engineName, String engineType) { this.engineName = engineName; this.engineType = engineType; } /** full constructor */ public Engine(Long engineId, String engineName, String engineType) { this.engineId = engineId; this.engineName = engineName; this.engineType = engineType; } // Property accessors public Long getEngineId() { return this.engineId; } public void setEngineId(Long engineId) { this.engineId = engineId; } public String getEngineName() { return this.engineName; } public void setEngineName(String engineName) { this.engineName = engineName; } public String getEngineType() { return this.engineType; } public void setEngineType(String engineType) { this.engineType = engineType; }public static void main(String[] args) throws Exception { Engine m = new Engine("汽车","发动机"); Class clazz = m.getClass(); Field[] f = clazz.getDeclaredFields(); String[] name = field2Name(f); Object[] value = field2Value(f, m); showNameAndValue(name, value); } public static void showNameAndValue(String[] name, Object[] value) { for (int i = 0; i < name.length; i++) { System.out.println("--" + i + "--"); System.out.println("name:" + name[i]); System.out.println("Value:" + value[i]); System.out.println("-- --"); } } public static String[] field2Name(Field[] f) { String[] name = new String[f.length]; for (int i = 0; i < f.length; i++) { name[i] = f[i].getName(); } return name; } public static Object[] field2Value(Field[] f, Object o) throws Exception { Object[] value = new Object[f.length]; for (int i = 0; i < f.length; i++) { value[i] = f[i].get(o); } return value; }}

如果想调用private的属性的属性值,则要调用他的get方法了具体做法是:

传奇手机游戏 源码,vscode和xshell,ubuntu黑屏卡死,tomcat入门 pdf,sqlite 快速入门,magento用什么服务器,下载广播插件,前端框架衣柜推荐实木,天津爬虫市场,php开发哪里培训,seo学习需要多长时间,旅游网站模板源码,flash单击按钮打开网页,简约网页模板免费,微信抢红包静态页面,志愿服务管理系统php,PJblog博客程序lzw

把field2Value方法改为如下方式,这样在别的类中也可以得到private的属性值了

积分商城php源码,ubuntu总是密码错误,nodejs爬虫点击页面,exlore php,湖北seo科技lzw

public static Object[] field2Value(Field[] fields, Object o) throws Exception { Object[] values = new Object[fields.length]; Class classType = o.getClass(); for (int i = 0; i < fields.length; i++) { String fieldName = fields[i].getName(); String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getMethodName = "get" + firstLetter + fieldName.substring(1); Method getMethod = classType.getMethod(getMethodName, new Class[] {}); values[i] = getMethod.invoke(o, new Object[] {}); } return values;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。