class Cell<e>{
private e element;
private Cell<e> next; //next私有
Cell(e ele){
this.element=ele;
}
Cell(e l,Cell<e> k){
Cell(l); //构造函数只能new时,才能调用改为 this.element=l;
this.next=k;
}
public Cell<e> getnext(){
return next;
}
public e getelement(){
return element;
}
}
public class test{
public static void main(String[] args){
Cell<Integer> l=new Cell<int>(123);//泛型中定义类型,必须是引用类型,int不是基本类型不属于引用类型。可改为Cell<Integer> l =new Cell<Integer>(123);
Cell<String> ele=new Cell<String>("awdadw",l);//泛型类一旦声明类型,那么定义固定,可以修改成 Cell<String> ele=new Cell<String>("awdadw",new Cell<String>("123"));
System.out.println(ele.getelement());
System.out.println(ele.getnext().getelement());
}
}
|