2019-08-06 15:19:50 3696瀏覽
今天千鋒扣丁學(xué)堂Java培訓(xùn)老師給大家分享一篇關(guān)于Java中方法名稱和泛型相同的用法,結(jié)合實(shí)例形式分析了泛型替代方法名稱的相關(guān)使用技巧,下面我們一起來看一下吧。
public class SupGent { public class A<E> { E t; public A( E t ) { this.t = t; } public E E() { //采用了泛型E,碰巧方法名稱也是E,只不過不要弄混淆,有點(diǎn)像宏替換 return t; } } public class B<E> extends A<E> { public B( E t ) { super(t); } } public static void main( String[] args ) { B<String> b = (new SupGent()).new B<String>("test"); System.out.println(b.E()); } }
test
public class SupGent { public class A<E> { E t; public A( E t ) { this.t = t; } public E String() { return t; } } public class B<E> extends A<E> { public B( E t ) { super(t); } } public static void main( String[] args ) { B<String> b = (new SupGent()).new B<String>("test"); System.out.println(b.String()); } }
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入JavaEE/微服務(wù)VIP免費(fèi)公開課】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>