Generic programing

 Definition of a Simple Generic Class

public class Aken<T>{

      private T first;

      priavte T second;

      public Aken(){

           first=null;

           second=null;

      }

      public T getFirst(){

           return first;

      }

      public T getsSecond(){

           return second;

      }

      public void setFirst(T first){

           this.first=first;

      }

      public void setSecond(T second){

           this.second=second;

      }

}

in order to restrict the T Type we can create Class as follow:

class AkenSupport<T extends List>{
 private T first;
    private T second;
    public AkenSupport(){
         first=null;
         second=null;
    }

    public T getFirst(){

         return first;

    }

    public T getsSecond(){

         return second;

    }

    public void setFirst(T first){

         this.first=first;

    }

    public void setSecond(T second){
     this.second=second;
    }
    public static void main(String[] args) {
     Aken<String> aken=new Aken<String>();
     aken.setFirst("hello");
     aken.setSecond("world");
    

 }
 
}

and  you may need more bounds you can :

class AkenSupport1<T extends List & Set>{
}

 

 

In summary,you need to remember these facts about translation of java generics;

There are no generics in the virtual machines,only ordinary classes and methods

All type parameters are replaced by their bounds;

Bridge methods are synthesized to preserve polymorplism

Casts are inserted as necessary to preserve type safety

Type Parameters Cannot Be Instantiated with Primitive Types

You Cannot Throw or Catch Instances of a Generic Class

You cannot Instantiate Type Variables

Type Variables are not valid in static contexts of generic classes

Wildcard Types

Aken<? extends ArrayList>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值