当前位置:网站首页>Two implementation methods of generic interface
Two implementation methods of generic interface
2022-06-12 15:18:00 【A_ M amu】
Generic interface
stay JDK1.5 after , Not only can you declare generic classes , You can also declare generic interfaces , The syntax for declaring generic interfaces is similar to that for declaring generic classes , Also add... After the interface name <T>, The format is as follows :
[ Access right ] interface The name of the interface < Generic identity >{
}
Two implementations of generic interfaces
After the generic interface is defined , To define subclasses of this interface , There are two ways to define subclasses of generic interfaces , One is to declare generics directly after subclasses , The other is to explicitly give generic types in the interface implemented by subclasses .
package com.am.demo05;
/**
* @program: java senior - Generic
* @description:
* @author: Amur
* @create: 2021-12-20 15:20
**/
public class Test05 {
public static void main(String[] args) {
USB<Integer> a=new Upan<>();
USB<String> b=new Mouse();
}
}
// Generic interface .
interface USB<T>{
public T show();
}
// This generic type is also used when creating classes
class Upan<T> implements USB<T>{
@Override
public T show() {
return null;
}
}
// Specify specific data types for generic interfaces when creating classes .
class Mouse implements USB<String>{
@Override
public String show() {
return null;
}
}Generic methods
All the generic operations learned earlier are to generize the entire class , But you can also define generic methods in classes .== The definition of a generic method has nothing to do with whether the class it belongs to is a generic class ==, The class can be generic , It can also not be a generic class .
The format of generic methods :
public < Generic identity > Generic identity Method name (){
}
public class Test {
public static void main(String[] args) {
String hello = fun("hello");
}
public static <T> T fun(T t){
return t;
}
}
边栏推荐
- Codeforces Round 797 (Div. 3,CF1690)全题解
- [LDA] basic knowledge notes - mainly AE and VAE
- 增加mysql的最大连接数
- IMU的学习记录
- Introduction to resttemplate
- Array related content
- [jvm learning] types of GC and allocation process of objects on JVM heap
- Two ways of array simulating queue
- Microservice fault tolerance
- Xshell 7 official website free download
猜你喜欢
![[LDA] LDA theme model notes - mainly Dirichlet](/img/e0/bc96b141aa577106379fab63d9df40.png)
[LDA] LDA theme model notes - mainly Dirichlet

ROS 中 boost::bind( ) 的使用

C string

TCP/IP 三次握手四次挥手(面试题)
![[LDA] rough version notes of EM variational reasoning [to be improved](/img/de/4b028e77694297db05a2d6b6589276.png)
[LDA] rough version notes of EM variational reasoning [to be improved
![[jvm learning] virtual machine stack](/img/64/4942c572f1ae4e4c6e2a6b657660e3.jpg)
[jvm learning] virtual machine stack

C main function

SOA Architecture

Deepin20.6 RTX3080 安裝顯卡驅動510.60.02、CUDA11.6、PyTorch1.11

NETCORE combined with cap event bus to realize distributed transaction -- Introduction (1)
随机推荐
分布式并发重复提交问题
Shardingsphere practice (6) - elastic scaling
Servlet连接数据库实现用户登录功能
Autofac Beginner (1)
Socket原理讲解(在哪、是什么、怎么用)
机器人前行、旋转的service编写
C main函数
Servlet connects to database to realize user login function
Ngork implements intranet penetration -- free
[LDA] LDA theme model notes - mainly Dirichlet
解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击
Preparation of service for robot moving forward and rotating
The process of generating strong association rules from frequent itemsets
How to write year-end summary
3D reconstruction system | L3 dual view motion recovery structure (SFM binocular SFM)
Pointer related concepts
PTA:自测-1 打印沙漏 (20分)
C string
PTA:自测-2 素数对猜想 (20分)
C constant, cannot be changed