泛型的使用

泛型



在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

泛型作为方法参数

public void sellGoods(List<? extends Goods> goods){
        // 调用集合中的sell方法
        for (Goods g:goods){
            g.sell();
        }
    }
 public static void main(String[] args) {
        List<Book> books=new ArrayList<>();
        books.add(new Book());
        books.add(new Book());
        List<Clother> clothers=new ArrayList<>();
        clothers.add(new Clother());
        clothers.add(new Clother());
        GoodsSeller goodsSeller=new GoodsSeller();
        goodsSeller.sellGoods(books);
        goodsSeller.sellGoods(clothers);
    }

自定义方式(上)

package day2;

public class NumberGenric<T> {
    private T num;
    public T getNum(){
        return num;
    }
    public void setNum(T num){
        this.num=num;
    }
    // test
    public static void main(String[] args) {
        // JDK7 后面可以省略
        NumberGenric<Integer> intNum=new NumberGenric<>();
        intNum.setNum(10);
        System.out.println("Intrger:"+intNum.getNum());

        NumberGenric<Float> floatNumberGenric = new NumberGenric<>();
        floatNumberGenric.setNum(5.0f);
        System.out.println("Float:"+floatNumberGenric.getNum());
    }
}

自定义泛型(下)

package day2;

public class NumberGeneric<T,X> {
    private T num1;
    private X num2;

    public void genNum(T num1,X num2){
        this.num1=num1;
        this.num2=num2;
    }
    public T getNum1() {
        return num1;
    }

    public void setNum1(T num1) {
        this.num1 = num1;
    }

    public X getNum2() {
        return num2;
    }

    public void setNum2(X num2) {
        this.num2 = num2;
    }

    public static void main(String[] args) {
        NumberGeneric<Integer,Float> numObj=new NumberGeneric<>();
        numObj.genNum(25,5.0f);
        System.out.println("num1="+numObj.getNum1()+",num2="+numObj.getNum2());
    }
}

自定义泛型方法

package day2;

public class GenericMethod {
   // <T extends Number> 添加限制
    public <T> void printValue(T t){
        System.out.println(t);
    }

    public static void main(String[] args) {
        GenericMethod gm=new GenericMethod();
        gm.printValue(123);
        gm.printValue("hello");
    }
}

练习作业

在这里插入图片描述

  • Animal
package day2;

public abstract class Animal {
    private String name;

    public Animal(){}
    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public abstract void play();
}

  • Dog
package day2;

public class Dog extends Animal{
    public Dog(String name){
        super(name);
    }
    @Override
    public void play() {
        System.out.println("修狗"+this.getName()+"在做游戏");
    }
}

  • Cat
package day2;

public class Cat extends Animal{
    public Cat(String name){
        super(name);
    }
    @Override
    public void play() {
        System.out.println("修猫"+this.getName()+"在做游戏");
    }
}

  • AnimalPlay
package day2;

import java.util.List;

public class AnimalPlay {
    public void test(List<? extends Animal> animals){
        for (Animal a:animals){
            a.play();
        }
    }
}

  • main
public static void main(String[] args) {
        List<Dog> dogs=new ArrayList<>();
        dogs.add(new Dog("巴豆"));
        dogs.add(new Dog("豆豆"));

        List<Cat> catList=new ArrayList<>();
        catList.add(new Cat("花花"));
        catList.add(new Cat("凡凡"));
        AnimalPlay animalPlay=new AnimalPlay();
        animalPlay.test(dogs);
        animalPlay.test(catList);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值