泛型:两个数字相加

定义:参数化类型 就是给类一个形参 类似于方法一样

           List<String> list  String是它的形参 而加入进去的具体字符串是他的实参


作用:进行类型检测 防止类型转换异常 将运行时的错误提前到编译期

List<String> list = new ArrayList<String>();
         list.add("wyq");
         list.add("hello");
         list.add(56);   // 这里编译错误

作用范围:泛型只作用于编译器 在运行期间不起作用 


通配符:Integer与Number存在继承关系, 但是List<Integer> 与List<Number>不存在继承关系,可以使用通配符实现继承关系extends上限 super下限

                例如  List<T extends Number> 所有继承Number的子类都可以存入这个List中

 <span style="font-family:Courier New;font-size:14px;">    public class Solution {
</span><span style="font-family: 'Courier New';font-size:14px;">       class MyNumber<T extends Number>{</span>
<span style="font-family:Courier New;font-size:14px;">        private T t;
        public void set</span><span style="font-family: 'Courier New'; font-size: 14px;">Number</span><span style="font-family: 'Courier New';">(T t){</span><span style="font-family:Courier New;font-size:14px;">
            this.t = t;
        }
        public T get</span><span style="font-family: 'Courier New'; font-size: 14px;">Number</span><span style="font-family: 'Courier New';">(){</span><span style="font-family:Courier New;font-size:14px;">
            return this.t;
        }
    }</span>
<span style="font-family:Courier New;font-size:14px;">    public static void main(String[] args){
      Solution s = new Solution();
      Solution.MyNumber p = s.new MyNumber<Integer>();//内部类的实例化方法
      p.setNumber(123);
      System.out.print((Integer)p.getNumber());</span><pre name="code" class="java"><span style="font-family:Courier New;font-size:14px;">  }</span>
<span style="font-family:Courier New;font-size:14px;">}</span>

 

泛型类:

<span style="font-family:Courier New;font-size:14px;">public class Person<T>{
private T t;
public void setPerson(T t){
  this.t = t;
 }
public T getPerson(){
  return this.t;
 }
}</span>


泛型方法:

实现两个数相加

<span style="font-family:Courier New;font-size:14px;">    /** 通过使用泛型 两个数字相加 无论什么类型都可以
     * @param t1
     * @param t2
     * @param <T>
     */
    public <T extends Number> void sum(T t1,T t2){
        if(t1.getClass()==Integer.class&&t2.getClass()==Integer.class){
            System.out.print("Integer:");
            System.out.print( t1.intValue() +  t2.intValue());
        }else if(t1.getClass()==Float.class&&t2.getClass()==Float.class){
            System.out.print("Float:");
            DecimalFormat format = new DecimalFormat(".00"); //保留两位小数
            System.out.print(format.format(t1.floatValue() + t2.floatValue()));
        }else if(t1.getClass()==Double.class&&t2.getClass()==Double.class){
            System.out.print("Double:");
            DecimalFormat format = new DecimalFormat(".000"); //保留三位小数
            System.out.print(format.format(t1.doubleValue() + t2.doubleValue()));
        } else if(t1.getClass()==Long.class&&t2.getClass()==Long.class){
            System.out.print("Long:");
            System.out.print( t1.longValue() + t2.longValue());
        }else if(t1.getClass()==Short.class&&t2.getClass()==Short.class){
            System.out.print("Short:");
            System.out.print( t1.shortValue() + t2.shortValue());
        }else if(t1.getClass()==Double.class||t2.getClass()==Double.class){
            System.out.print("强制转换Double:");
            DecimalFormat format = new DecimalFormat(".000");
            System.out.print(format.format(t1.doubleValue() + t2.doubleValue()));
        }else if(t1.getClass()==Long.class||t2.getClass()==Long.class){
            System.out.print("强制转换Long:");
            System.out.print(t1.longValue() + t2.longValue());
        } else if(t1.getClass()==Float.class||t2.getClass()==Float.class){
            System.out.print("强制转换Float:");
            DecimalFormat format = new DecimalFormat(".00");
            System.out.print(format.format(t1.floatValue() + t2.floatValue()));
        }else if(t1.getClass()==Integer.class||t2.getClass()==Integer.class){
            System.out.print("强制转换Integer:");
            System.out.print(t1.intValue() + t2.intValue());
        }
    }</span>




  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值