Java中方法名称和泛型相同的用法示例
作者:cakincqm 时间:2023-08-12 03:18:55
本文实例讲述了Java中方法名称和泛型相同的用法。分享给大家供大家参考,具体如下:
一 点睛
Java中,方法的名称可以用泛型替代。
二 实战
1 代码
public class SupGent {
public class A<E> {
E t;
public A( E t ) {
this.t = t;
}
public E E() { //采用了泛型E,碰巧方法名称也是E,只不过不要弄混淆,有点像宏替换
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());
}
}
2 运行
test
3 说明
和下面代码等价
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());
}
}
更多java相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/93368291
标签:Java,方法名称,泛型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java创建与结束线程代码示例
2023-01-16 16:20:00
IDEA导入外部项目报Error:java: 无效的目标发行版: 11的解决方法
2021-08-25 03:02:27
![](https://img.aspxhome.com/file/2023/4/62704_0s.png)
java编译时出现使用了未经检查或不安全的操作解决方法
2022-05-21 00:21:21
![](https://img.aspxhome.com/file/2023/0/114050_0s.jpg)
Java实战之用springboot+netty实现简单的一对一聊天
2023-12-03 07:28:19
![](https://img.aspxhome.com/file/2023/0/61080_0s.png)
SpringMVC的执行过程浅析
2021-05-31 20:51:11
![](https://img.aspxhome.com/file/2023/4/62074_0s.png)
Java实现中国象棋的示例代码
2021-08-10 21:56:03
![](https://img.aspxhome.com/file/2023/2/63432_0s.jpg)
详解spring mvc中url-pattern的写法
2023-11-11 07:30:58
WPF中鼠标/键盘/拖拽事件以及用行为封装事件详解
2021-05-24 00:22:38
![](https://img.aspxhome.com/file/2023/2/126052_0s.gif)
Java实现员工信息管理系统
2023-01-27 16:19:30
![](https://img.aspxhome.com/file/2023/0/81940_0s.jpg)
Android Activity之间的数据传递方法总结
2023-05-26 03:43:21
![](https://img.aspxhome.com/file/2023/4/139474_0s.jpg)
Kotlin实现图片选择器的关键技术点总结
2023-03-05 02:31:57
![](https://img.aspxhome.com/file/2023/0/118980_0s.jpg)
使用Maven搭建Hadoop开发环境
2021-09-11 07:55:45
使用Swing绘制动态时钟
2023-11-24 07:05:12
![](https://img.aspxhome.com/file/2023/6/59166_0s.gif)
java生成json数据示例
2023-10-30 04:06:45
![](https://img.aspxhome.com/file/2023/9/64059_0s.jpg)
java中hasNextInt判断后无限循环输出else项的解决方法
2023-11-15 10:12:44
一篇文章带你搞定JAVA注解
2023-03-15 05:22:23
![](https://img.aspxhome.com/file/2023/2/63012_0s.png)
如何利用泛型封装通用的service层
2023-05-15 04:55:43
Spring框架基于注解开发CRUD详解
2021-08-04 03:39:03
![](https://img.aspxhome.com/file/2023/7/111447_0s.jpg)
Spring一步到位精通拦截器
2022-01-03 18:31:50
![](https://img.aspxhome.com/file/2023/5/68165_0s.png)
IntelliJ IDEA的代码搁置功能实现
2022-03-28 07:32:52
![](https://img.aspxhome.com/file/2023/1/74921_0s.png)