Home >Java >javaTutorial >About generic classes, generic methods, and generic interfaces in java
Generic class:
public class FanXingLeiDemo
private T obj;
public T getObj() {
return obj ;
}
public void setObj(T obj) {
this.obj = obj;
}
}
Implementation:
public class FanXingMain {
public static void main (String[] args) {
FanXingLeiDemo
fxd.setObj("hahaha");
System.out.println(fxd.getObj());
}
}
Generic method:
public class FanXingMethod {
public
{
System.out.println(t);
}
}
Implementation:
public class FangXingMethodMain {
public static void main(String[] args) {
FanXingMethod fxm = new FanXingMethod();
fxm.show(100);
fxm.show("shshh ");
fxm.show(true);
}
}
Generic interface:
public interface Inter
public abstract void show(T t);
}
Implement interface:
public class InterImpl
@Override
public void show(T t) {
//TODO automatically generated method stub
System. out.println(t);
}
}
Instantiation:
public class InterMain {
public static void main(String[] args) {
Inter
in.show("hahah");
}
}
More about generic classes, generic methods, generics in java For interface-related articles, please pay attention to the PHP Chinese website!