s-day02-泛型接口的使用

This commit is contained in:
2026-07-31 11:13:44 +08:00
parent e6ac76cd25
commit 6bdf2742a2
4 changed files with 75 additions and 0 deletions
@@ -0,0 +1,12 @@
package com.inmind.generic_interface07;
public class Demo10 {
public static void main(String[] args) {
//创建实现类1对象
MyInterfaceImpl myInterface = new MyInterfaceImpl();
myInterface.method("hello");
//创建实现类2对象(在实现子类中不确定类型,在创建实现类对象时才确定类型)
MyInterfaceImpl2<Integer> myInterface2 = new MyInterfaceImpl2<>();
myInterface2.method(123);
}
}
@@ -0,0 +1,19 @@
package com.inmind.generic_interface07;
/*
泛型接口:在接口中定义一个未知类型
泛型接口的格式:
public interface 接口名<大写字母>{
}
泛型接口的泛型类型何时确定???
1.在定义实现子类中直接确定泛型的类型
2.在实现子类中不确定类型,在创建实现类对象时才确定类型
*/
public interface MyInterface<T> {
//泛型作为参数
void method(T t);
//泛型作为返回值
T method1();
//泛型既作为参数又作为返回值
T method2(T t);
}
@@ -0,0 +1,21 @@
package com.inmind.generic_interface07;
/*
泛型接口的泛型类型何时确定???
1.在定义实现子类中直接确定泛型的类型
*/
public class MyInterfaceImpl implements MyInterface<String>{
@Override
public void method(String s) {
}
@Override
public String method1() {
return "";
}
@Override
public String method2(String s) {
return "";
}
}
@@ -0,0 +1,23 @@
package com.inmind.generic_interface07;
/*
泛型接口的泛型类型何时确定???
2.在实现子类中不确定类型,在创建实现类对象时才确定类型
*/
public class MyInterfaceImpl2<E> implements MyInterface<E>{
@Override
public void method(E e) {
}
@Override
public E method1() {
return null;
}
@Override
public E method2(E e) {
return null;
}
}