泛型概述 为什么要使用泛型 在Java中增加泛型之前,泛型程序设计使用继承来实现的
坏处: 需要强制转换 可向集合中添加任意类型的对象,存在风险
泛型的使用 List list=new ArrayList(); javaSE7及以后的版本中,构造方法中可以省略泛型类型 List list=new ArrayList<>();
多态与泛型 class Animal{} class Cat extends Animal{} List list=new ArrayList(); //这是不允许的,变量声明的类型必须匹配传递给实际对象的类型 其它错误的例子: List list=new ArrayList(); List number=new ArrayList();
泛型作为方法参数 1 2 3 4 5 6 7 8 9 10 11 package com.imooc.generic;public class Shoes extends Goods {@Override public void sell () { System.out.println("sell shoes " ); } }
1 2 3 4 5 package com.imooc.generic;public abstract class Goods { public abstract void sell () ; }
1 2 3 4 5 6 7 8 9 10 11 package com.imooc.generic;public class Book extends Goods {@Override public void sell () { System.out.println("sell books" ); } }
1 2 3 4 5 6 7 8 9 10 package com.imooc.generic;public class Clothes extends Goods {@Override public void sell () { System.out.println("sell clothes" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 package com.imooc.generic;import java.util.List;public class GoodsSeller { public void sellGoods (List<? extends Goods> goods) { for (Goods g:goods){ g.sell(); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.imooc.generic;import java.util.ArrayList;import java.util.List;public class GoodsTest {public static void main (String[] args) { List<Book> books=new ArrayList<Book>(); books.add(new Book()); books.add(new Book()); List<Clothes> clothes=new ArrayList<Clothes>(); clothes.add(new Clothes()); clothes.add(new Clothes()); List<Shoes> shoes=new ArrayList<>(); shoes.add(new Shoes()); shoes.add(new Shoes()); GoodsSeller goodsSeller=new GoodsSeller(); goodsSeller.sellGoods(books); goodsSeller.sellGoods(clothes); goodsSeller.sellGoods(shoes); } }
编程练习 1通过练习,掌握泛型作为方法参数的应用,具体内容见任务要求。运行效果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package me.feihong.generic;public abstract class Animal { private String name; public String getName () { return name; } public void setName (String name) { this .name = name; } public abstract void game () ;}
1 2 3 4 5 6 7 8 9 10 11 package me.feihong.generic;public class Dog extends Animal {@Override public void game () { System.out.println("小狗" +getName()+"在做游戏!" ); } }
1 2 3 4 5 6 7 8 9 10 11 package me.feihong.generic;public class Cat extends Animal {@Override public void game () { System.out.println("小猫" +getName()+"在做游戏!" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 package me.feihong.generic;import java.util.List;public class AnimalPlay { public void playGame (List<? extends Animal> animal) { for (Animal an:animal) { an.game(); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package me.feihong.generic;import java.util.ArrayList;import java.util.List;public class AnimalTest {public static void main (String[] args) { List<Cat> cat=new ArrayList<Cat>(); Cat cat1=new Cat(); cat1.setName("花花" ); Cat cat2=new Cat(); cat2.setName("凡凡" ); cat.add(cat1); cat.add(cat2); List<Dog> dog=new ArrayList<Dog>(); Dog dog1=new Dog(); dog1.setName("巴迪" ); Dog dog2=new Dog(); dog2.setName("豆豆" ); dog.add(dog1); dog.add(dog2); AnimalPlay play=new AnimalPlay(); play.playGame(dog); play.playGame(cat); } }
自定义泛型(上) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 一个泛型类定义好之后怎样使用 public class NumGeneric<T> { private T num; public T getNum(){ return num; } public void setNum(T num){ this.num = num; } //以下为测试 NumGeneric<Integer> intNum = new NumGeneric<>(); intNum.set(10); NumGeneric<Float> floatNum = new NumGeneric<>(); floatNum.setNum(5.0f); //泛型类的定义和使用,可以传进不同类的对象作为参数 }
自定义泛型(下) 1 2 3 4 5 6 7 8 9 10 11 泛型类中两个参数的情况 public class TwoNumGeneric<T,X>{ private T num1; private X num2; 构造函数以及set&get略 public static void main(String[] args){ TwoNumGeneric<Integer,Float> numObj = new TwoNumGeneric<>(); numObj.genNum(25,50f); } }
自定义泛型方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 *泛型方法不一定在泛型类里面 public class GenericMethod{ public <T> void printValue(T t){ 内容略 } //或者也可以加入限制 public <T extends Number> void printValue(T t){ 内容略 } GenericMethod gm = new GenericMethod(); gm.printValue("hello"); }