java编程基础课程-300集史上最全、最深、最细内容摘要:

E 等泛型字母为 有类型, 类型参数赋予具体的值 –。 未知类型 类型参数赋予丌确定值 , 任意类型 • 只能用在声明类型 、方法参数上, 丌能用在定义泛型类上 • 通配符 (代码片段 ) //此处 T不能够换成 ? public class StudentT { T score。 public static void main(String[] args) { //通配符 :用在声明类型 Student? stu = new StudentString()。 test(new StudentInteger())。 } //通配符 :用在声明方法参数 接收信息 public static void test(Student? stu){ //获取信息 ()。 } } 上限 • 上限 extends :指定的类型必须是继承某个类,或者实现某个接口 (丌是用implements),即 =如 – ? extends Fruit – T extends List • 丌能添加信息 • 存在以下规则 ,如 – ListFruit 满足 List? extends Fruit – List? extends Apple 满足 List? extends Fruit – List?等同 List? extends Object public void test(List? extends Fruit list){ (new Fruit(“f))。 (new Pear(“p))。 (new Apple(“a))。 } 以上代码无法通过编译。 为什么呢。 public class FooT extends List { public static void main(String[] args) { //上限 FooArrayList f =new FooArrayList。 FooLinkedList f2 =new FooLinkedList。 } } 下限 • 下限 super:指定的类型丌能小于操作的类 ,即 = – T super Apple –。 super Apple • 丌能添加父对象 • 存在以下规则 ,如 – List? super Fruit 满足 List? super Apple – ListFruit 满足 List? super Fruit public void test(List? super Apple list){ (new Apple(“apple))。 (new FujiApple(“fuji))。 (new Fruit(“fruit”))。 } 以上代码哪一行不能通过编译。 为什么呢。 泛型嵌套 • 稍微复杂一些,从外到内拆分 public class StudentT { T score。 } public static void main(String[] args) { //泛型的嵌套 BjsxtStudentString room =new BjsxtStudentString()。 //从外到内拆分 = new StudentString()。 StudentString stu =。 String score =。 (score)。 } public class Bjsxt T{ T stu。 } 其他 • 泛型没有多态 • 没有泛型数组 • jdk7简化泛型 public class Jdk7{ public static void main(String[] args) { // ListString arrList= new ArrayListString()。 //,声明类型即可,使用 |创建时不用指定类型 ListString arrList2= new ArrayList()。 } } public class OthersApp { public static void。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。