Generic programing
来源:互联网 发布:python apply async 编辑:程序博客网 时间:2024/05/22 02:25
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>
- Generic programing
- 2013.3.31 generic programing
- Java 的泛型程序设计( Generic Programing)
- Generic
- Generic
- generic
- C programing
- Dynamic Programing
- concurrence programing
- Programing TIPS
- Functional Programing
- Agile Programing
- shell programing
- java application programing
- Over 200 Programing books
- OO Programing in C
- flash game programing university
- My life of programing
- VC调用外部程序
- 怎么从EXCEL或WORD里提取图片?
- 电脑高手应如是(转)
- SQLServer2000数据库自动备份
- hibernate的数据库访问例子收集
- Generic programing
- java生成验证图片,很好用的
- 如何做好开发团队负责人---管别人先管好自已,请从做一名合格程序员开始
- 毕业5年决定你的命运
- 读书总结1
- Eclipse快捷键大全
- 一年中的第n天是几月几号?
- pku-3624
- c中的数组名的指针类型解析