有勇气的牛排博客

java 泛型接口

有勇气的牛排 1048 Java 2021-08-26 21:57:34

1 定义

格式: 修饰符 interface4 接口名<类型名>{ }

范例:public interface Generic<T>{ }

2 代码实战

2.1 泛型接口

Generic.java

package itheima_04; /** * 泛型接口 * */ public interface Generic<T> { void show(T t); }

2.2 泛型接口实现类

GenericImpl.java

package itheima_04; /** * 泛型接口实现类 */ public class GenericImpl<T> implements Generic<T> { // 重写方法 public void show(T t) { System.out.println(t); } }

2.3 测试类

GenericDemo.java

package itheima_04; /** * 测试类 * */ public class GenericDemo { public static void main(String[] args) { GenericImpl<String> g1 = new GenericImpl<String>(); g1.show("有勇气的牛排"); GenericImpl<Integer> g2 = new GenericImpl<Integer>(); g2.show(20); } }

留言

专栏
文章
加入群聊