有勇气的牛排博客

java 泛型方法

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

1 泛型方法

package itheima_03; //public class Generic { // public void show(String s){ // System.out.println(s); // } // // public void show(Integer i){ // System.out.println(i); // } // // public void show(Boolean b){ // System.out.println(b); // } //} //// 泛型类改进 //public class Generic<T> { // public void show(T t) { // System.out.println(t); // } //} // 泛型方法改进 // 调用方法的那一刻 才去明确类型 public class Generic { public <T> void show(T t) { System.out.println(t); } }

2 测试类

package itheima_03; /** * 测试类 * */ public class GenericDemo { public static void main(String[] args) { Generic g = new Generic(); g.show("有勇气的牛排"); g.show(20); g.show(true); g.show(6.6); } }

留言

专栏
文章
加入群聊