美文网首页
(1)泛型类和泛型接口

(1)泛型类和泛型接口

作者: 卡戎li | 来源:发表于2017-05-01 09:49 被阅读0次

泛型的好处

  • 类型安全
  • 消除强制转换
package generics;

/**
 * Created by lipei on 2017/5/1.
 */
public class NoGenericsDemo {

    public static void main(String[] args) {

        Wrapper w1 = new Wrapper(123);
        Wrapper w2 = new Wrapper("1234");
        Wrapper w3 = new Wrapper(Long.valueOf("1234567890"));

        System.out.println(w1);
        System.out.println(w2);
        System.out.println(w3);

        //放进去没有错误,取出也没有问题,但是运行时就会出问题
        String w11 = (String) w1.getContent();
        System.out.println("w11 =" + w11);

    }
}

class  Wrapper{
    public Object content;

    public Wrapper(Object content) {
        this.content = content;
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Wrapper{" +
                "content=" + content +
                '}';
    }
}

什么是泛型

  • 实现了参数化类型的概念,是代码可以应 用于多种类型
  • 可以用在类、接口和方法的创建中,分别 称为泛型类、泛型接口、泛型方法
  • 当创建类型化参数时,编译器会负责转换 操作

通过泛型优化

package generics;

/**
 * Created by lipei on 2017/5/1.
 */
public class GenericsDemo {

    public static void main(String[] args) {

        Wrapper02<Integer>w1 = new Wrapper02(123);
        Wrapper02 w2 = new Wrapper02("1234");
        Wrapper02 w3 = new Wrapper02(Long.valueOf("1234567890"));

        System.out.println(w1);
        System.out.println(w2);
        System.out.println(w3);

        //在类型初始化的时候已经告知是什么类型,故运行时不会出错
        Integer w11 =   w1.getContent();
        System.out.println("w11 =" + w11);

    }
}

class  Wrapper02<T>{
    public T content;

    public Wrapper02(T content) {
        this.content = content;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Wrapper{" +
                "content=" + content +
                '}';
    }
}

声明与使用泛型接口

package generics;

/**
 * Created by lipei on 2017/5/1.
 */
public interface GenericsInterface<T> {
    public T update (T t);
}


class  test01 implements  GenericsInterface <String>{
    @Override
    public String update(String s) {
        return null;
    }
}


class  test02 implements  GenericsInterface<Integer>{

    @Override
    public Integer update(Integer integer) {
        return null;
    }
}

相关文章

  • 泛型

    一、泛型类、泛型接口和泛型方法1、泛型接口的定义: 2、两种泛型类的定义:(1) (2) 3、泛型方法的定义,可以...

  • 泛型

    1.什么是泛型? 2.为什么需要泛型? 3.泛型类、接口 泛型类注意事项: 从泛型类派生子类 泛型接口 泛型接口的...

  • 【泛型】通配符与嵌套

    上一篇 【泛型】泛型的作用与定义 1 泛型分类 泛型可以分成泛型类、泛型方法和泛型接口 1.1 泛型类 一个泛型类...

  • 重走安卓进阶路——泛型

    ps.原来的标题 为什么我们需要泛型? 泛型类、泛型接口和泛型方法(泛型类和泛型接口的定义与泛型方法辨析); 如何...

  • 三泛型的使用

    1.泛型类和泛型方法 (1)格式 (2)示例例如a.泛型类 b.实现方法 2.泛型接口和泛型方法 (1)格式 (2...

  • 泛型的使用

    泛型有三种使用方式,分别为:泛型类、泛型接口、泛型方法 泛型类 泛型接口 泛型通配符 泛型方法 静态方法与...

  • Java 19-5.1泛型

    泛型类定义泛型类可以规定传入对象 泛型类 和泛型方法 泛型接口 如果实现类也无法确定泛型 可以在继承类中确定泛型:

  • 泛型(持续更新中......)

    一、泛型类和泛型接口 1.如果定义了泛型类,实例化没有指明类的泛型,则认为此泛型类型为Object类型。2.要求:...

  • typescript入门-泛型

    基础定义 泛型接口 泛型类 泛型约束 使用类类型

  • 泛型

    关于泛型类,泛型方法,泛型接口泛型类:public class FanXingLeiDemo {//这里是泛...

网友评论

      本文标题:(1)泛型类和泛型接口

      本文链接:https://www.haomeiwen.com/subject/ireftxtx.html