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

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值