//在类声明中使用泛型
package fx;
import java.util.*;
class Studentfx
{
}
class Worker
{
}
class Tool<E> //E是指定为泛型
{
private E obj;
public void setobject(E obj)
{
this.obj=obj;
}
public E getObject()
{
return obj;
}
}
public class TestFXL {
public static void main(String[] args) {
Tool<Studentfx> tool=new Tool<Studentfx>();
tool.setobject(new Studentfx());
Studentfx worker=tool.getObject();
/*Tool tool=new Tool();
tool.setobject(new Studentfx());
Object obj=tool.getObject();
Worker worker=(Worker)obj;
System.out.println(worker);//编译通过执行异常
*/
}
}
package fx;
import java.util.*;
class Studentfx
{
}
class Worker
{
}
class Tool<E> //E是指定为泛型
{
private E obj;
public void setobject(E obj)
{
this.obj=obj;
}
public E getObject()
{
return obj;
}
}
public class TestFXL {
public static void main(String[] args) {
Tool<Studentfx> tool=new Tool<Studentfx>();
tool.setobject(new Studentfx());
Studentfx worker=tool.getObject();
/*Tool tool=new Tool();
tool.setobject(new Studentfx());
Object obj=tool.getObject();
Worker worker=(Worker)obj;
System.out.println(worker);//编译通过执行异常
*/
}
}