进阶day02-泛型接口的使用

This commit is contained in:
2026-01-31 14:11:09 +08:00
parent 787e103e2e
commit 6d0a68cf23
4 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
package com.inmind.generic_interface05;
public class Demo06 {
public static void main(String[] args) {
//创建实现类2对象
MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>();
impl2.method1(1);
Integer i = impl2.method2();
}
}

View File

@@ -0,0 +1,25 @@
package com.inmind.generic_interface05;
/*
泛型接口:在接口中定义一个未知的类型
泛型接口的格式:
public interface 接口名<T> {
}
泛型接口的泛型类型何时确定???
1.在定义实现子类中直接确定接口的类型
2.在实现子类中不确定类型,在创建该实现子类对象时才确定
*/
public interface MyInterface<T> {
//泛型作为参数
void method1(T t);
//泛型作为返回值
T method2();
//泛型既作为参数,又作为返回值
T method3(T t);//是不是泛型方法?????
}

View File

@@ -0,0 +1,20 @@
package com.inmind.generic_interface05;
//1.在定义实现子类中直接确定接口的类型
public class MyInterfaceImpl1 implements MyInterface<String>{
@Override
public void method1(String s) {
}
@Override
public String method2() {
return "";
}
@Override
public String method3(String s) {
return "";
}
}

View File

@@ -0,0 +1,19 @@
package com.inmind.generic_interface05;
//2.在实现子类中不确定类型,在创建该实现子类对象时才确定
public class MyInterfaceImpl2 <T> implements MyInterface<T> {
@Override
public void method1(T t) {
}
@Override
public T method2() {
return null;
}
@Override
public T method3(T t) {
return null;
}
}