java反射机制
来源:互联网 发布:linux syslog server 编辑:程序博客网 时间:2024/06/06 21:04
参考文章:http://blog.csdn.net/justinavril/article/details/2873664
测试代码:
创建类:
Convey.java
package com.domain;import java.util.HashSet;import java.util.Set;/** * Convey entity. @author MyEclipse Persistence Tools * 啊是的撒F多少 */public class Convey implements java.io.Serializable {// Fieldsprivate Integer id;private String name;private Double price;private Set order = new HashSet(0);// Constructors/** default constructor */public Convey() {}/** minimal constructor */public Convey(String name, Double price) {this.name = name;this.price = price;}/** full constructor */public Convey(String name, Double price, Set order) {this.name = name;this.price = price;this.order = order;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public Double getPrice() {return this.price;}public void setPrice(Double price) {this.price = price;}/** * sad * @return */public Set getOrder() {return this.order;}public void setOrder(Set order) {this.order = order;}}
测试类:
Start.java
package com.domain;import java.beans.MethodDescriptor;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Start {/** * @param args * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */public static void main(String[] args) throws Exception{// TODO Auto-generated method stub//获得类中的方法Class class1=Convey.class;//实例化对象Object object=class1.newInstance();System.out.println("类名:"+class1.getName());System.out.println("包名:"+class1.getPackage());//获得字段System.out.println("字段:");Field [] field=class1.getDeclaredFields();//class1.getFields();System.out.println(field.length);for (Field field2 : field) {System.out.println(field2.getName()+" type:"+field2.getType());}//方法System.out.println("全部方法");Method [] methods= class1.getMethods();for (Method method : methods) {System.out.println(method.getName());}//获得公开的方法System.out.println("公开的方法");Method [] pMethods= class1.getDeclaredMethods();for (Method method : pMethods) {System.out.println(method);}//获得释文System.out.println("释文"); Annotation[] annotation= class1.getDeclaredAnnotations(); for (Annotation annotation2 : annotation) {System.out.println(annotation2);} System.out.println("操作对象");//invoke方法 Class partypes[] = new Class[1]; //参数数量 partypes[0] = String.class; //参数类型 Method meth = class1.getMethod("setName", partypes); //赋值 Object arglist[] = new Object[1]; arglist[0]="你好啊!"; //调用方法 Object retobj = meth.invoke(object, arglist); //调用方法 返回结果 meth = class1.getMethod("getName", new Class[0]); retobj= meth.invoke(object, new Class[0]); System.out.println(retobj);}}主要作用说明:
获取指定类中的所有字段、方面、实例化类、设置属性、调用方法
- 【反射】JAVA反射机制
- 【Java】JAVA反射机制
- Java 反射机制[Field反射]
- Java 反射机制[Method反射]
- Java反射机制笔记-反射机制
- java的反射机制
- Java的反射机制
- java反射机制详解!
- Java反射机制
- Java的反射机制
- java 反射机制--侯捷
- java反射机制
- java反射机制
- [候捷]Java反射机制
- java 反射机制
- java 反射机制初探
- 关于Java反射机制
- java反射机制
- Zimbra用户对群组发送邮件的权限设置
- windows 下的嵌入式开发
- 删除tableview的section遇到crash:The number of rows contained in an existing section after the update
- you must specify a host type if you use `--no-verify'
- 随想录(写给那些学校不是985、211的同学们)
- java反射机制
- [小代码]通过IP和端口连接到远程摄像机
- 错误解决:Install ncurses(ncurses-devel) and try again
- vim 处理字符编码
- 事物
- 利用 Oracle 系统触发器防止误删除操作
- oracle审计详解
- 换行处理
- 机器学习博客