A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hadexs 中级黑马   /  2013-5-26 08:50  /  1501 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

泛型的组合应用?给个实例!

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

5 个回复

倒序浏览
  1. package lib;

  2. import java.util.*;
  3. class Hello<T> {
  4.         private T first;

  5.         Hello(T first) {
  6.                 this.first = first;
  7.         }

  8.         @Override
  9.         public String toString() {
  10.                 // TODO Auto-generated method stub
  11.                 return "first:" + first;
  12.         }
  13. }
  14. public class test {
  15.         public static void main(String[] args) {
  16.                 Hello<String> h1 = new Hello<String>("小明");
  17.                 Hello<Integer> h2 = new Hello<Integer>(22);
  18.                 Hello<Object> h3 = new Hello<Object>("ssss");
  19.                 List<Hello<?>> l1 = new ArrayList<Hello<?>>();// ?通配符
  20.                 l1.add(h1);
  21.                 l1.add(h2);
  22.                 l1.add(h3);
  23.                
  24.                 List<Hello<? extends Number>> l2 = new ArrayList<Hello<? extends Number>>(); // 通配符上限
  25.                 // l2.add(h1);//报错,因为String不是Number的子类
  26.                 l2.add(h2);
  27.                 //l2.add(h3);    //报错,因为Object不是Number的子类

  28.                 List<Hello<? super String>> l3 = new ArrayList<Hello<? super String >>();// 通配符下限
  29.                 l3.add(h1);
  30.                 //l3.add(h2);   //报错 此处只能使用String 或者Object
  31.                 l3.add(h3);
  32.                

  33.         }
  34. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

回复 使用道具 举报
泛型是个神马东东,这个我也要学习下哦
回复 使用道具 举报
/**
* @author Rollen-Holt 使用泛型
*/
  
class hello<T, V> {
    hello(){
  
    }
  
    public T getName(){
        return name;
    }
  
    public void setName(T name){
        this.name = name;
    }
  
    public V getAge(){
        return age;
    }
  
    public void setAge(V age){
        this.age = age;
    }
  
    private T name;
    private V age;
  
    public static void main(String[] args){
        hello<String, Integer> he = new hello<String, Integer>();
        he.setAge(10);
        he.setName("Rollen Holt");
        System.out.println(he.getName() + "   " + he.getAge());
    }
}
  
/**
* @author Rollen-Holt 泛型类的构造方法定义
*/
  
class hello<T, V> {
  
    hello(T name, V age){
        this.age = age;
        this.name = name;
    }
  
    public T getName(){
        return name;
    }
  
    public V getAge(){
        return age;
    }
  
    private T name;
    private V age;
  
    public static void main(String[] args){
        hello<String,Integer> he=new hello<String,Integer>("Rollen",12);
        System.out.println(he.getName()+"  "+he.getAge());
    }
}
  
/**
* @author Rollen-Holt 使用通配符
*/
  
class info<T> {
    info(T name){
        this.name = name;
    }
    private T name;
}
  
class hello{
    public static void function(info<?> temp){
        System.out.println("内容: "+temp);
    }
      
    public static void main(String[] args){
        info<String> demo=new info<String>("Rollen");
        function(demo);
    }
}
  
/**
* @author Rollen-Holt 泛型上限
*/
  
class info<T>{
    info(T age){
        this.age=age;
    }
    private T age;
}
  
class hello{
    public static void function(info<? extends Number> temp){
        System.out.println("内容"+ temp);
    }
      
    public static void main(String[] args){
      
      info<Integer> demo=new info<Integer>(1);
      function(demo);
    }
}
回复 使用道具 举报
接上面,一条装不下




  
/**
* @author Rollen-Holt 泛型下限
*/
  
class info<T>{
    info(T age){
        this.age=age;
    }
    private T age;
}
  
class hello{
    public static void function(info<? super String> temp){
        System.out.println("内容"+ temp);
    }
      
    public static void main(String[] args){
      
     // 此处只能使用String 或者Object
      info<String> demo=new info<String>("Rollen");
      function(demo);
    }
}
/**
* @author Rollen-Holt 泛型和子类继承的限制
*/
  
class info<T>{
}
  
class hello{
    public static void main(String[] args){
    info<String> demo1=new info<String>();
    info<Object> demo2=new info<Object>();
    //demo2=demo1;   此处错误
      
    }
}
  
/**
* 上面的例子说明,一个类的子类可以通过多态性被其父类实例化
* 但是在泛型操作中,子类的泛型类型是无法被其父类的泛型类型实例化的。
*/
如果允许上面的条语句的话,会出现:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:  
    Type mismatch: cannot convert from info<String> to info<Object>
  
    at hello.main(hello.java:12)
泛型接口的两种实现:
/**
* @author Rollen-Holt 泛型接口的实现1
*/
  
interface info<T> {
    public void say();
}
  
// 直接在子类之后声明泛型
class hello<T> implements info<T>{
  
    public static void main(String[] args){
        info<String> demo = new hello<String>();
        demo.say();
  
    }
  
    public void say(){
        System.out.println("hello");
  
    }
}
  
/**
* @author Rollen-Holt 泛型接口的实现2
*/
  
interface info<T> {
    public void say();
}
  
// 在子类实现的接口中明确给出泛型类型
class hello implements info<String>{
  
    public static void main(String[] args){
        info<String> demo = new hello();
        demo.say();
  
    }
  
    public void say(){
        System.out.println("hello");
  
    }
}
/**
* @author Rollen-Holt 使用泛型通一传入参数的类型
*/
  
class info<T> {
    private T var;
  
    public T getVar(){
        return var;
    }
  
    public void setVar(T var){
        this.var = var;
    }
    public String toString(){
        return this.var.toString();
    }
      
}
class hello{
    public static void main(String[] args){
        info<String> demo1=new info<String>();
        info<String> demo2=new info<String>();
        demo1.setVar("Rollen");
        demo2.setVar("Holt");
        printAdd(demo1, demo2);
    }
    // 此处传递的都是同一个String类型的
    public static <T> void printAdd(info<T> demo1, info<T> demo2){
        System.out.println(demo1.getVar()+" "+demo2.getVar());
    }
}
否则的话如下所示:出现错误:
/**
* @author Rollen-Holt 使用泛型通一传入参数的类型
*/
  
class info<T> {
    private T var;
  
    public T getVar(){
        return var;
    }
  
    public void setVar(T var){
        this.var = var;
    }
    public String toString(){
        return this.var.toString();
    }
      
}
class hello{
    public static void main(String[] args){
        info<String> demo1=new info<String>();
        info<Integer> demo2=new info<Integer>();
        demo1.setVar("Rollen");
        demo2.setVar(30);
        //此处调用错误
        printAdd(demo1, demo2);
    }
    // 此处传递的都是同一个String类型的
    public static <T> void printAdd(info<T> demo1, info<T> demo2){
        System.out.println(demo1.getVar()+" "+demo2.getVar());
    }
}
  
Exception in thread "main" java.lang.Error: Unresolved compilation problem:  
    The method printAdd(info<T>, info<T>) in the type hello is not applicable for the arguments (info<String>, info<Integer>)
  
    at hello.main(hello.java:26)

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

回复 使用道具 举报
HM张博文 发表于 2013-5-26 11:14
泛型是个神马东东,这个我也要学习下哦

你不会?都高级了亲
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马