Java 泛型,安卓面试项目经验
public static void main(String[] args) {
Tool<Student> tool = new Tool<>();
tool.setType(new Student());
Student student = tool.getType();
}
}
public class Tool<T> {
private T t;
public T getType() {
return t;
}
public void setType(T t) {
this.t = t;
}
}
public class Student extends Person {
public Student() {
}
public Student(String name, int age) {
super(name, age);
}
}
public class Person implements Comparable<Person> {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person person) {
int temp = this.age - person.age;
return temp == 0 ? this.name.compareTo(person.name) : temp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
2、泛型方法
public class Tool<T> {
private T t;
public T getType() {
return t;
}
public void setType(T t) {
this.t = t;
}
/**
将泛型定义在方法上
@param str
@param <W>
*/
public <W> void show(W str) {
System.out.println("show: " + str);
}
public void print(T str) {
System.out.println("print: " + str);
}
/**
当方法静态时,不能访问类上定义的泛型。如果静态方法使用泛型,只能将泛型定义在方法上
@param str
@param <Y>
*/
public static <Y> void method(Y str){
System.out.println("method: " + str);
}
}
public class GenericDemo5 {
public static void main(String[] args) {
Tool<String> tool = new Tool<>();
tool.show(4);
tool.print("haha");
Tool.method("hehehe");
Tool.method(88);
}
}
3、泛型接口
public class GenericDemo6 {
public static void main(String[] args) {
InterImpl inter = new InterImpl();
inter.show("abc");
InterImpl2<String> inter2 = new InterImpl2<String>();
inter2.show("abc222");
}
}
interface Inter<T> {
void show(T t);
}
class InterImpl implements Inter<String> {
@Override
public void show(String s) {
System.out.println("show:" + s);
}
}
class InterImpl2<T> implements Inter<T> {
@Override
public void show(T t) {
System.out.println("show:" + t);
}
}
4、通配符的体现
public class GenericDemo7 {
public static void
main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("abc");
arrayList.add("haha");
ArrayList<Integer> arrayList2 = new ArrayList<>();
arrayList2.add(111);
arrayList2.add(222);
printCollection(arrayList);
printCollection(arrayList2);
}
/**
泛型的通配符:?
@param collection
*/
private static void printCollection(Collection<?> collection) {
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
5、泛型限定
? extends E:接受E类型或者其子类型对象 上限!
? super E: 接受E类型或者其父类型对象 ?下限!
一般存储元素的时候都是用上限,因为这样取出的都是按照上限类型来运算的,不会出现类型安全隐患。
public class GenericDemo8 {
public static void main(String[] args) {
ArrayList<Worker> arrayList = new ArrayList<>();
arrayList.add(new Worker("张三",27));
arrayList.add(new Worker("李四",30));
ArrayList<Student> arrayList2 = new ArrayList<>();
arrayList2.add(new Student("小明",17));
arrayList2.add(new Student("小芳",18));
printCollection(arrayList);
printCollection(arrayList2);
}
/**
泛型的通配符:?
泛型的高级应用:泛型限定
@param collection
*/
private static void printCollection(Collection<? extends Person> collection) {
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
public class Student extends Person {
public Student() {
}
public Student(String name, int age) {
super(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + getName() + ''' +
", age=" + getAge() +
'}';
}
}
评论