Home > Java > javaTutorial > body text

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

高洛峰
Release: 2016-12-19 15:40:54
Original
1284 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!