反射机制
来源:互联网 发布:企业域名注册流程 编辑:程序博客网 时间:2024/06/05 04:34
1,自动生成get,set方法;
用Myeclipse开发java web程序,写javabean的时候,如果字段很多的话,写get和set方法是一件很无语和浪费时间的事情,所以Myeclipse提供了一个自动生成这些方法的功能。
2,一个类的常用生成方法
package com.yiji;public class test1 {private String brand;private String color;private int maxSpeed;public test1(){}public test1(String brand, String color, int maxSpeed){this.brand =brand;this.color = color;this.maxSpeed = maxSpeed;}public void introduce(){System.out.println("brand:"+brand);}public static void main(String[] args) {test1 car = new test1("红旗", "黑色",100);car.introduce();}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public int getMaxSpeed() {return maxSpeed;}public void setMaxSpeed(int maxSpeed) {this.maxSpeed = maxSpeed;}}
3,用反射法
package com.yiji;import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class Car {public static test1 initByDefaultConst() throws Throwable{//通过类装载器获得test1对象ClassLoader loader = Thread.currentThread().getContextClassLoader();Class clazz = loader.loadClass("com.yiji.test1");//获取类的默认构造器对象并通过它实例化对象Constructor cons = clazz.getDeclaredConstructor((Class[])null);test1 car = (test1)cons.newInstance();//通过反射方法设置属性Method setBrand = clazz.getMethod("setBrand", String.class);setBrand.invoke(car,"红旗");return car;}public static void main(String[] args)throws Throwable{test1 car = initByDefaultConst();car.introduce();}}
0 0
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- 反射机制
- extjs4 ComboBox 动态加载,联动,设置默认值,锁定等
- C dairy 1
- Java Eclipse IDE 设置
- zanotti femme but I'm not."Although there has been media exposure
- 邀请好友我赚了1026个集分宝 分享庆祝下
- 反射机制
- 在Linux下使用vim配合xxd查看并编辑二进制文件
- white toms I saw two about 40 meters.
- Linux-HA开源软件Heartbeat(配置篇)
- JFreeChart笔记 (三) 时间序列图:设置图表显示属性
- toms wedges how it will because A small parking problems
- C常见库函数实现方法——legend
- SAP 中的popup dialog (弹出对话框) 常见实现方法
- louboutin homme