Skip to content

Latest commit

 

History

History
66 lines (42 loc) · 1.39 KB

File metadata and controls

66 lines (42 loc) · 1.39 KB

有限定的通配符

只允许泛型为Person 及其子类的引用调用 只允许泛型为Person 及其超类的引用调用  只允许泛型为实现Comparable接口的实现类的引用调用 ## 编写泛型 ### 泛型方法 在返回值前写上泛型参数: ~~~java public void test(T s){} public String test2(){} ~~~ 泛型方法在调用前没有固定的参数类型 在调用时,变成固定的类型 #### 静态方法 无法在方法参数和返回类型上使用泛型,把静态方法改为“泛型方法”(code 2 改写 ) `Noted!!!:` ~~~java //code 1 public static SomeClass method(T first,T Secend){ ... return new SomeClsaa(first, secend) } //无法通过编译 ~~~ 会出现的`类似`泛型: ~~~Java public static SomeClass method(T first,T Secend){ ... return new SomeClsaa(first, secend) } ************************************************************ // static 的和 SomeClass 没有任何关系 // 静态泛型方法与使用其他类型区分开 public static SomeClass method(K first,K Secend){ ... return new SomeClsaa(first, secend) } ~~~ ### 泛型接口 继承泛型接口: 传入泛型参数不需要在类上使用泛型 未传入泛型必须和继承接口的泛型参数保持一致