内省 & 泛型

在面向对象的过程中,通常用一个个bean来封装对象的属性。
首先要明白,一个bean是否有属性和有多少个属性,均是由它有多少个get或者set属性决定的。例如:

package introspector;

public class Person {

    String name;
    int age;
    String password;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getAB(){
        return null;
    }
}

在此例子中,一共就有五个属性,三个字段。
五个属性的来源是:
一个是因为继承了Object里面的getClass()方法,有一个Class属性
一个是因为有一个getAB()方法,有一个AB属性
还有三个就是get和set指定的属 性

操纵bean的方法:

  • introspector内省,这种方法是在做框架时使用的,不是常规方法

    操作的步骤是:
    通过introspector获取某一个bean的BeanInfo
    通过PropertyDescriptor获取相应的方法
    invoke一把,实现方法,实现bean内属性的操作
    实例如下:

package introspector;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.junit.Test;
import org.junit.internal.runners.statements.InvokeMethod;

public class demo1 {
    @Test  //通过此测试,可以获得所有属性的名字,可以测试出一共有多少的属性
    public void tset1() throws Exception{
        BeanInfo info = Introspector.getBeanInfo(Person.class);//获得BeanInfo
        PropertyDescriptor[] pd = (PropertyDescriptor[])
                                info.getPropertyDescriptors();//获得所有的属性
        for(PropertyDescriptor p : pd){//打印所有的属性名
            System.out.println(p.getName());
        }
        System.out.println("******");
        //还可以只打印自己属性而不打印继承来的属性:
        BeanInfo info1 = Introspector.getBeanInfo(Person.class, Object.class);//获得BeanInfo
        PropertyDescriptor[] pd1 = (PropertyDescriptor[])
                                info.getPropertyDescriptors();//获得所有的属性
        for(PropertyDescriptor p : pd){//打印所有的属性名
            System.out.println(p.getName());
        }

    }

    @Test
    public void test2() throws Exception{

        Person p = new Person();
        PropertyDescriptor pd = new PropertyDescriptor("name", Person.class);
        //获得name属性的描述器
        Method me1 = pd.getWriteMethod();//获得set方法
        me1.invoke(p, "nihao");

        Method me2 = pd.getReadMethod();//获得get方法
        System.out.println(me2.invoke(p, null));
    }

}

泛型:
泛型的目的是为了在编译阶段就发现错误,在编译成.class文件之后,是无区别的。举例List和Map如下:

package generic;

import java.awt.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;


public class demo1 {

    @Test
    public void tst1(){
        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("aaa");
        list1.add("bb");
        //传统方式:
        Iterator<String> it = list1.iterator();
        while(it.hasNext()){//通过迭代器实现
            String value = it.next();
            System.out.println(value);
        }

        //增强for
        for(String me: list1){
            System.out.println(me);//
        }
    }

    @Test
    public void test2(){
        Map<Integer,String> map = new LinkedHashMap<Integer,    String>();
        map.put(1, "value1");
        map.put(2, "value2");
        map.put(3, "value3");
        //传统方式两种:keyset 和 entry
        Set<Entry<Integer, String>> set = map.entrySet();
        Iterator<Entry<Integer, String>> it = set.iterator();
        while(it.hasNext()){
            Entry<Integer, String> entry = it.next();
            int key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }


        //增强for
        for(Entry<Integer, String> e: set){
            int key = e.getKey();
            String value = e.getValue();
            System.out.println(key + "=" + value);
        }
    }

}

注意事项:
在使用泛型时,两边必须保持一致;如下几种是错误的示例:
ArrayList ar = new ArrayList();
ArrayList ar = new ArrayList();

但是在只有一边的时候却是可以的,这是为了兼容性;如下是正确的示例:
ArrayList ar = new ArrayList();
ArrayList ar = new ArrayList();

还有就是泛型参数不能是基本数据类型:
ArrayList ar = new ArrayList();是对的
ArrayList ar = new ArrayList();是错的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值