Java的泛型

一、泛型 :Generic
1、使用泛型创建一个类

//创建泛型类
public class fanxingExample <E>{
private E name;

public E getName() {
    return name;
}

public void setName(E name) {
    this.name = name;
}
}
//在主方法可以创建一个数值对象 或者 字符串对象
//使用泛型就可以输出数值 或者 字符串
//fanxingExample<String> s = new fanxingExample<>();

fanxingExample<Integer> INT = new fanxingExample<>();
INT.setName(10);
System.out.println(INT.getName());//10

2、用泛型创建一个方法
格式:
修饰符列表 <泛型> 返回值类型 方法名(参数列表(使用泛型)){//方法体
}

public class GenericMethod {
 //定义一个普通方法
   // public static void method1(参数列表)

    //定义一个泛型方法
    public <E> void method1(E e){
        System.out.println(e);
    }

    //定义一个静态的泛型方法
    public static <S> void method2 (S s){}
}
}
public static void main(String[] args) {
    //因为是方法用的泛型
    //所以只要创建一个普通对象
    GenericMethod ger = new GenericMethod();

    //因为方法创建了泛型,可以随意输入数据类型
    ger.method1("nnn");
    ger.method1(6.66);
    ger.method1(10);
    System.out.println(ger);
}

3、创建泛型接口
两种方法:
(1)、创建泛型接口,在实现类指定接口的泛型
(只要指定接口是什么泛型,但是要具体指定什么数据类型,输出什么数据)
(2)、创建泛型接口,接口使用什么泛型,实现类就用什么泛型
(接口和实现类都要指定,不需要确定数据类型)

public interface GenericInterface<I>{

    //创建了一个泛型接口
    public abstract void method1(I i);
}
//指定接口数据类型是字符串
public class GenericInterfaceImplement
 implements GenericInterface<String> {
    @Override
    public void method1(String i) {
       System.out.println(i);
    }
}
//实现类和接口都指定泛型
public class GenericInterfaceImplement01<I> 
implements GenericInterface<I> {
    @Override
    public void method1(I i) {
        System.out.println(i);
    }
}
/*
这是错误写法
实现类没有创建泛型
public class GenericInterfaceImplement01 
implements GenericInterface<S>
 */
public static void main(String[] args) {

    //方法一,定义了接口的泛型的对象打印输出

    GenericInterfaceImplement GER =
     new GenericInterfaceImplement();
   // GER.method1(22);这是错误的;因为泛型定义了一个字符串
    GER.method1("只能来一个字符串");//只能来一个字符串

    //方法二,接口与实现类都创建泛型对象打印输出
    //这里是定义了一个<Interger>
    GenericInterfaceImplement01<Integer> int1 =
     new GenericInterfaceImplement01<>();
    int1.method1(200);//200

    GenericInterfaceImplement01<String> STR = 
    new GenericInterfaceImplement01<>();
    STR.method1("来上一个字符串");//来上一个字符串
}

4、泛型的通配符
?:代表任意的数据类型
使用方式:
只能作为方法参数,而不能创建对象

public static void main(String[] args) {

    ArrayList<Integer> integers = new ArrayList<>();
    integers.add(10);
    integers.add(20);

    ArrayList<String> str = new ArrayList<>();
    str.add("aa");
    str.add("ab");

    GenericMethod(integers);
    GenericMethod(str);

    //从上面可以看出到底使用什么数据类型,
    //只要定义一个方法就能遍历所有集合
    //这时就可以使用通配符
}
public static void GenericMethod (ArrayList<?>arrayList){
    //使用迭代器进行遍历
    Iterator<?> gen = arrayList.iterator();
    while (gen.hasNext()){
        Object nex = gen.next();
        System.out.println(nex);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值