Android 泛型使用

泛型 本质上就是参数类型化,

 

// 泛型类创建 (T是一个标记号)
public class Goh {

    T instance;

    private T get() {
        return instance;
    }

    private void set(T instance) {
        this.instance = instance;
    }

}

// 泛型接口
public interface Wlf {
    T mandate();
    int ip(T item);
}


// 在正常开发中经常出现多层接口,如果想继承父接口的泛型需要这样写,代表继承父接口泛型
public interface WlfChina extends Wlf {
    String getClub(T brand);
}

// 多个类型参数
public interface WlfApply {
    public void put(K key, V value);
    public V getV(K key);
}

泛型到底有什么用?

① 编译时进行更强的类型检查(检测到更多错误,从而提高了代码的稳定性)

② 自动强制转换

// 泛型上限可指定多个,如果上限里面有类,必须放在最前面,接口无所谓
class TallFirst{
}

 未完,持续更新。

你可能感兴趣的:(android,泛型,java泛型,android泛型)