不说了,直接上代码:
public class NewTest { public static void main(String[] args) {
N<Integer> n1 = new N<Integer>(1) {
@Override
void handler() {
System.out.println("这个是数字:" + this.getContent()); } };
n1.handler(); var n2 = new N<String>("这个是字符串,用var定义临时变量哦") {
@Override
void handler() {
System.out.println("这个是字符串文本哦:" + this.getContent()); }
};
n2.handler(); /*我可以匿名哦,JDK9支持哦*/
N<?> n3 = new N<>("object") {
@Override
void handler() {
System.out.println("object:"+this.getContent());
}
};
n3.handler();
} } abstract class N<T> { private T content; public N(T content) {
this.content = content;
} public T getContent() {
return content;
} public void setContent(T content) {
this.content = content;
} abstract void handler(); }