前置知识:重载中的参数类型不包含泛型:
// package 泛型;
public class index {
public static void main (String[] args){
test<Integer> t = new test<> ();
t.setContent(999);
int a = (Integer) t.getContent();
System.out.println(a);
// int n = 10;
a(t);
}
public static void a(test<String> n){
System.out.println(n.getContent());
}
public static void a(test<Integer> n){
System.out.println(n.getContent());
}
}
class test<T>{
T content;
public static void main (String[] args) throws Exception{
}
public void setContent(T content) {
this.content = content;
}
public T getContent() {
return content;
}
}
编译结果 | 误: 名称冲突: a(test<Integer>)和a(test<String>)具有相同疑符 public static void a(test<Integer> n){ |
package 泛型;
public class index {
public static void main (String[] args){
test<String> t = new test<> ();
t.setContent("999");
String a = t.getContent();
// System.out.println(a);
// int n = 10;
a(t);
}
public static void a(test n){
n.setContent(888);
System.out.println(n.getContent());
}
}
class test<T>{
T content;
public static void main (String[] args) throws Exception{
}
public void setContent(T content) {
this.content = content;
}
public T getContent() {
return content;
}
}
如果 a 方法参数不 指明具体类型(此时默认描述为 Object 类型) :代码可以正常 输出 【888】。但是意味着 泛型类没有起到任何作用,所以此时泛型引用传递的最佳方案就是:可以接受已所走的泛型对象、但是不允许内容的修改。而为了实现这样的需求,可以使用 【 ?】作为通配符进行控制 。
public static void a(test<?> n){
n.setContent(888);
System.out.println(n.getContent());
}
编译 | 错误: 不兼容的类型: int无法转换为CAP#1 n.setContent(888); ^ 其中, CAP#1是新类型变量: CAP#1从?的捕获扩展Object |
此时,在 a方法中使用了 “ ?” 作为了泛型的通配符,这样可以接受所有的泛型类型的对象实例
但是不允许进行内容的修改,而只是完成内容的获取。而在 “ ?” 通配符的操作基础之上,Java又给出了两个子通配符的处理格式:
【类和方法】 设置泛型的上限 ( ? extents 类):只能够使用当前类或者当前类的子类作为泛型类型;【 ?extends Number】:可以使用Number或者是其子类实现泛型的定义。
【方法】 设置泛型的下限 (?super 类):只能够设置指定类或其父类;【?super String】:可以使用String 或其父类作为泛型类型。
上限报错:
package 泛型;
public class index {
public static void main (String[] args){
test<String> t = new test<> ();
// t.setContent("999");
// String a = t.getContent();
// System.out.println(a);
// int n = 10;
a(t);
}
public static void a(test<? extends Number> n){
// n.setContent("888");
System.out.println(n.getContent());
}
}
class test<T extends Number>{
T content;
public static void main (String[] args) throws Exception{
}
public void setContent(T content) {
this.content = content;
}
public T getContent() {
return content;
}
}
编译 | |
不兼容的类型: 无法推断test<>的类型参数 test<String> t = new test<> (); ^ 原因: 推论变量 T 具有不兼容的上限 等式约束条件:String 下限:Number 其中, T是类型变量: T扩展已在类 test中声明的Number |
下限报错:
package 泛型;
public class index {
public static void main (String[] args){
test<Integer> t = new test<> ();
t.setContent(999);
// String a = t.getContent();
// System.out.println(a);
// int n = 10;
a(t);
}
public static void a(test<? super String> n){
// n.setContent("888");
System.out.println(n.getContent());
}
}
class test<T>{
T content;
public static void main (String[] args) throws Exception{
}
public void setContent(T content) {
this.content = content;
}
public T getContent() {
return content;
}
}
编译 | 错误: 不兼容的类型: test<Integer>无法转换为test<? super String> a(t); ^ |
通过这种上限和 下限的处理 可以使得在进行 泛型参数 接收的时候更加方便的实现数据类型的控制。