Home  >  Article  >  Java  >  About generic classes, generic methods, and generic interfaces in java

About generic classes, generic methods, and generic interfaces in java

高洛峰
高洛峰Original
2016-12-19 15:40:541222browse

Generic class:

public class FanXingLeiDemo {//This is a generic type, you can pass any parameters


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 = new FanXingLeiDemo();

fxd.setObj("hahaha");

System.out.println(fxd.getObj());

}

}

Generic method:

public class FanXingMethod {

public void show(T t)

{

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 implements Inter {


@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 = new InterImpl< ;String>();

in.show("hahah");


}

}



More about generic classes, generic methods, generics in java For interface-related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn