杭州seo网站优化公司,旅游海外推广网站建设方案,现在什么类型网站没有人做,胶州城阳网站建设泛型 泛型的本质#xff1a;参数类型化 概述#xff1a;将类型由原来的具体的类型参数化#xff0c;然后在 使用/调用 时传入具体的类型 格式#xff1a; 类型 指定一种类型的格式#xff0c;这里的类型可以看成是 方法中的形参#xff08;如果不理解可去看下形…泛型 泛型的本质参数类型化 概述将类型由原来的具体的类型参数化然后在 使用/调用 时传入具体的类型 格式 类型 指定一种类型的格式这里的类型可以看成是 方法中的形参如果不理解可去看下形参和实参并且 类型 只能是 引用类型不能是基本数据类型 好处 把运行时期的问题提前到了编译期间 import java.util.ArrayList;
import java.util.Collection;public class Test{// 遍历集合public static void show(Collection co){for(Object c : co){System.out.println(c);}}public static void main(String args[]){// 1、创建集合对象,Collection 是接口所以创建对象只能用其子类实现类Collection c new ArrayList();// 2、添加元素c.add(1);c.add(张三);c.add(true);// 3、遍历集合show(c);}
}注意我们可以看到此时的 Collection 集合可以添加任意数据类型但是大家思考一个问题集合中存储不同数据类型是否会影响我们后期对于集合中数据的操作呢答案是肯定的怎么解决呢我们就可用到泛型。 这样我们就可以将 问题 提前到了 编译期间 避免了强制类型转换 泛型类 格式 修饰符 class 类名参数类型{} // 学生泛型类
class StudentT{private T value; // 定义私有变量public Student(T value){this.value value;}public T getValue(){return value;}public void setValue(T value){this.value value;}
}
// 测试类
public class Test1{public static void main(String args[]){// 这里的 中 String 和 Integer 是实参T 是形参StudentString s1 new StudentString(张三);StudentInteger s2 new StudentInteger(1);System.out.println(s1.getValue() s2.getValue());}
}最终运行结果张三 1 泛型方法 格式修饰符 类型 返回值类型 方法名类型 变量名{} class Cat{// 泛型方法public T T show(T t){return t;}
}
public class Test{public static void main(String args[]){// 1、创建对象Cat t new Cat();// 2、调用泛型方法,传入不同的参数String s t.show(张三);int i t.show(1);System.out.println(s i);}
}最终运行结果张三 1 泛型接口 格式修饰符 interface 接口名 类型{} interface AnimalT{// 抽象方法public void show(T t);
}// 实现 泛型接口
class Dog implements AnimalString{Overridepublic void show(String s) {System.out.println(s);}
}public class Test{public static void main(String[] args) {Dog d new Dog();d.show(李四);}
}类型通配符 类型通配符? 例如List?表示元素类型未知的List,他的元素可以匹配任何的类型 这种带通配符的List仅表示它是各种泛型的List的父类并不能把元素添加到其中 范围格式举例作用任意类型?List?表示的类型是所有上限? extends 类型List? extends Number表示的类型是Number或者其子类下限? super 类型List? super Number表示的类型是Number或者其父类