zoukankan      html  css  js  c++  java
  • java 泛型接口示例

    /*
     * 泛型接口
     */
    interface Tool<t> {
        public void show(T t);
         
        //泛型方法
        public <e> void print(E e);
    }
     
    /*
     * 这种方式适合实现接口的时候就知道类里的泛型是什么
     */
    class ToolImpl implements Tool<string> {
     
        @Override
        public void show(String t) {
            System.out.println("show:" + t);
        }
     
        @Override
        public <e> void print(E e) {
            System.out.println("print:" + e);
        }
     
    }
     
    /*
     * 这种方式不好!
     */
    class Tool2Impl implements Tool {
     
        @Override
        public void show(Object t) {
            System.out.println("show obj" + t);
        }
     
        @Override
        public void print(Object e) {
            System.out.println("show obj" + e);
        }
     
    }
     
    /*
     * 这种方式适合使用的时候指定泛型 
     * 在继承接口的时候不用指定泛型 
     * 注意的是<t>也可以写成别的比如<c> 
     * 写成<e>的话,类上的<e>和print方法
     * 上的<e>也不是一个类型
     */
    class Tool3Impl<t> implements Tool<t> {
     
        @Override
        public void show(T t) {
            System.out.println("show=" + t);
        }
     
        @Override
        public <e> void print(E e) {
            System.out.println("print=" + e);
        }
     
    }
     
    /*
     * 这个写法中show方法和print方法用的泛型也不是一个 
     * 类上的<e>和print方法上的<e>不是一个类型!!!
     */
    class Tool4Impl<e> implements Tool<e> {
     
        @Override
        public void show(E t) {
            System.out.println("show-" + t);
        }
     
        @Override
        public <e> void print(E e) {
            System.out.println("print-" + e);
        }
     
    }
     
    /*
     * 错误!
     * class Tool5Impl<string> implements Tool<t> {
     * 
     * }
     */
    /*
     * 正确,但是这个泛型上的String没意义,和Tool3Impl写法没区别 
     * class Tool5Impl<string, t=""> implements Tool<t> {
     * 
     * }
     */
    /*
     * 错误1 
     * class Tool5Impl<string|t> implements Tool<e> {
     * 
     * }
     */
    /*
     * 正确,但是这个泛型上的String|T没意义,和Tool4Impl写法没区别 
     * class Tool6Impl<string|t, e=""> implements Tool<e> {
     * 
     * }
     */
    public class GenericDemo {
     
        public static void main(String[] args) {
            ToolImpl ti = new ToolImpl();
            ti.show("nihao");
            ti.print(6);
            Tool3Impl<string> t3i = new Tool3Impl<string>();
            t3i.show("haha");
            t3i.print(6);
            Tool4Impl<string> t4i = new Tool4Impl<string>();
            t4i.show("hehe");
            t4i.print(6);
        }
     
    }
    

      

  • 相关阅读:
    SpringMVC中@Controller和@RequestMapping用法和其他常用注解
    eclipse maven install 时控制台乱码问题解决
    使用模板实现编译期间多态(类名当参数)
    QT中Dialog的使用(使用QStackedWidget维护页面切换)
    QT中的各种对话框
    Qt 5 最小构建笔记(只编译QtBase)
    忽然懂了:“视图”的用途不仅仅是临时表,更适用于变化比较大的情况,而且不用改客户端一行代码
    React-Native
    一位OWin服务器新成员TinyFox
    Access Toke调用受保护的API
  • 原文地址:https://www.cnblogs.com/jifeng/p/5798869.html
Copyright © 2011-2022 走看看