BeanUtils、反射、装箱、泛型

 package cn.demo02;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import cn.demo.Dog;
import cn.demo.Person;
public class Demo02 {
 @Test
 public void test1() throws Exception {
  Dog dog = new Dog();
  // 通过BeanUtils的方法来设置属性
  BeanUtils.setProperty(dog, "addr", "中国北京");// 没有成对出现也可以设置成功
  BeanUtils.setProperty(dog, "money", "45.9");// 自动类型转换成对应的类型
  BeanUtils.setProperty(dog, "mail", " 没有mail这个属性,也不出错
  BeanUtils.setProperty(dog, "age", new String[] { "100", "300" });// 数组也可以自动的转换成int类型只转换下标为0的值
  System.err.println(dog);
  String money = BeanUtils.getProperty(dog, "age");
  System.err.println("money:" + money);
 }
 @Test
 public void test2() throws Exception {
  Person p = new Person();
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("name", "Jerry");
  map.put("age", "45");
  map.put("addr.tel", "190893883");
  BeanUtils.populate(p, map);
  System.err.println(p);
 }
 @Test
 public void test3() throws Exception {
  List<String> list = new ArrayList<String>();
  list.add("Jack");
  // 反射
  List.class.getMethod("add", Object.class).invoke(list, 90);
  // 地址引用
  List list2 = list;
  list2.add(45.3D);
  for (Object obj : list) {
   System.err.println(obj + "," + obj.getClass());
  }
  
  
  String i = say("Jack");
  Integer ii = say(34);
  
  One<String> on = new One<String>();
  on.say("Jack");
  
  Integer iii=90;//装箱
  int a = new Integer(190);
 }
 public <T> T say(T t) {
  return t;
 }
}
class One<T>{//不能用于静态的方法上
 public T say(T t){
  return t;
 }
}

你可能感兴趣的:(BeanUtils、反射、装箱、泛型)