Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

java 中泛型设计的字母意思

T表示的是类型(type)那么

U,A,B,C 表示的是什么呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

泛型主要还是起约束作用。简单给个示例,希望对你有帮助 :

public class Test1 {
    @Test
    void test() {
        Sample<String, String, Boolean, Boolean, Long, Long> sample = new Sample();
        sample.a = "string";
        sample.b = "string";
        sample.t = true;
        sample.u = false;
        sample.ab = 123L;
        sample.cde = 345L;

        Sample<Boolean, Boolean, Long, Long, String, String> sample1 = new Sample();
        sample1.a = true;
        sample1.b = false;
        sample1.t = 123L;
        sample1.u = 345L;
        sample1.ab = "string";
        sample1.cde = "string";
    }
}

/**
 * 泛型主要用于进行约束,实际使用中常用T,U,V等,实际上可以使用任意值.
 */
class Sample<A, b, T, u, AB, cdE> {
    //a的类型声明为A,对应Sample上的A
    // 所以在使用时字段a的类型必须与声明sample时的第一个泛型类型相同
    public A a;
    public b b;
    public T t;
    public u u;
    public AB ab;
    public cdE cde;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...