A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 范龙彬 黑马帝   /  2011-11-3 23:39  /  2137 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

泛型类可以继承吗     如果可以说明一下是怎么实现的。还有 父类成员又是怎么调用  请举出个例子  我是真的糊涂了  !

该贴已经同步到 范龙彬的微博

4 个回复

倒序浏览
可以被继承,代码是网上找的希望对你有所帮助:
class Father<T> {

    T t;
    Father(T t) {
        this.t = t;
    }

    public void fatherPrint() {
        System.out.println("father's " + t);
    }
}

class Son<T> extends Father {

    Son(T t) {
        super(t);
    }

    public void sonPrint() {
        System.out.println("son's " + t);
    }
}

public class Test {

    public static void main(String[] args) {
        Father<Integer> f = new Son<Integer>(new Integer(4));
        f.fatherPrint();
        Son<Integer> s = (Son<Integer>) f;
        s.sonPrint();
    }
}
回复 使用道具 举报
可以被继承,代码是网上找的希望对你有所帮助:
class Father<T> {

    T t;
    Father(T t) {
        this.t = t;
    }

    public void fatherPrint() {
        System.out.println("father's " + t);
    }
}

class Son<T> extends Father {

    Son(T t) {
        super(t);
    }

    public void sonPrint() {
        System.out.println("son's " + t);
    }
}

public class Test {

    public static void main(String[] args) {
        Father<Integer> f = new Son<Integer>(new Integer(4));
        f.fatherPrint();
        Son<Integer> s = (Son<Integer>) f;
        s.sonPrint();
    }
}
回复 使用道具 举报
继承说白了就是在超类的基础上,多加几个自己的方法而已,所以和泛型不泛型没关系。
至于如何实现超类方法,那就是需要你在构造函数中传递参数了,所以子类构造函数一般有super();
回复 使用道具 举报
提醒一下:泛型限定不考虑继承关系。
就是比如一个类定义为 class ClassTest<Object>{...}
你创建时用 new ClassTest<String>是不行的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马