java泛型、通配符、泛型数据和方法的用例代码

来源:互联网 发布:java date 加减 编辑:程序博客网 时间:2024/05/18 15:06
<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">import org.omg.CORBA.portable.UnknownException;import org.omg.IOP.Encoding;import org.omg.PortableInterceptor.INACTIVE;import java.util.HashMap;import java.util.Map;/** * Created by cuboo on 2016/9/28. */public class fanxing {    public static void main(String agrs[]){        fans<String> f = new fans<String>("100","200");        System.out.println(f.getX()+"  "+f.getY());        fans<Integer> f1 = new fans<Integer>(10,20);        System.out.println(f1.getX()+"  "+f1.getY());        fan1<String,Integer> f2 = new fan1<String,Integer>();        f2.put("heo",100);        System.out.println(f2.get("heo"));        fans<Integer> f3 = new fans<Integer>(101,201);        told(f3);        fan fn = new fan();        System.out.println(fn.fanl(156));        System.out.println(fn.fanl("hello fan"));        fn.fan1(new Integer[]{1,2,3,4});        fn.fan1(new String[]{"h","e","l","l","o"});    }    //通配符?的使用    public static void told(fans<?> f){        System.out.println(f.getX()+"  "+f.getY());    }}//泛型方法class fan{    public  <T>T fanl(T str){        return str;    }    //泛型数据    public  <T>void fan1(T[] a){            for(T i : a){            System.out.println(i);        }    }}//泛型类<大写字母>一般写<T> class fans<B>{    fans(B x,B y){   //构造方法中使用        this.x = x;        this.y = y;    }    private B x;    private B y;    public void setX(B x) {        this.x = x;    }    public B getX() {        return x;    }    public void setY(B y) {        this.y = y;    }    public B getY() {        return y;    }}//多个泛型的类class fan1<K,V>{    private K key;    private V value;    public void put(K key,V value){        this.key = key;        this.value = value;    }    public V get(K key) {            if (this.key == key){                return value;            }      return null;    }}


                                             
0 0