演示代码:
package exercise;import java.util.Arrays;public class MyArrayList<E> {Object[] obj = new Object[10];int size;public boolean add(E e) {obj[size] = e;size++;return true;}public E get(int index) {return (E) obj[index];}//没有这个函数,无法看到obj内部的值public String toString() {return Arrays.toString(obj);}
}
测试:
package exercise;public class Test {public static void main(String[] args) {MyArrayList<String> list = new MyArrayList<>();list.add("sunshine");list.add("jiuselu");list.add("kukuhsui");System.out.println(list);System.out.println(list.get(0));}
}
代码演示:
package exercise;import java.util.ArrayList;public class ListUtil {private ListUtil() {}//泛型要写在修饰符后,public、static都是,写在后面的修饰符后。public static <E> void addAll(ArrayList<E> list, E e1, E e2, E e3, E e4) {list.add(e1);list.add(e2);list.add(e3);list.add(e4);}//可以实现任意参数加入集合public static<E> void addAll1(ArrayList<E> list,E...e){for (E element : e) {list.add(element);}}}
测试类:
package exercise;import java.util.ArrayList;public class Test {public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();ListUtil.addAll(list,"sunshine","jiuselu","lulushui","zechao");System.out.println(list);ArrayList<String> list1 = new ArrayList<>();ListUtil.addAll1(list1,"sunshine");System.out.println(list1);}
}
代码演示:
方法一:
方法二:
package exercise;public class Test {public static void main(String[] args) {//泛型接口的两种使用方式//1.实现类给出的具体类型MyArrayList list = new MyArrayList();list.add("sunshine");//2.实现类延续泛型,创建实现类对象时再确定类型MyArrayList1<String> list1 = new MyArrayList1<>();list1.add("jiuselu");}
}
package exercise;import java.util.ArrayList;public class Demo1 {public static void main(String[] args) {//泛型不具备继承性,但数据具备继承性ArrayList<Ye> list1 = new ArrayList<>();ArrayList<Fu> list2 = new ArrayList<>();ArrayList<Zi> list3 = new ArrayList<>();method1(list1);method1(list2);method1(list3);list1.add(new Ye());list1.add(new Fu());list1.add(new Zi());method2(list1);method2(list2);method2(list3);}/*此时,泛型里面写的是什么类型,那就只能传递什么类型的数据弊端:利用泛型方法有一个小弊端,它可以接受任意类型数据比如 Ye Fu Zi希望:本方法虽然不确定类型,但是我希望洗后只传递Ye Fu Zi此时,我们就可以使用泛型的通配符:?:表示不确定的类型它可以进行类型的限定? extend E :表示可以传递E或者E所有子类类型? super E :表示可以传递E和E的所有父类类型应用场景:1.如果我们在定义类、方法、接口的时候,如果类型不确定,就可以定义泛型类、泛型方法、泛型接口。2.如果类型不确定,但是能知道以后只能传递某个继承体系中的,就可以使用泛型的通配符泛型的通配符:关键点:可以限定类型的范围*/public static void method1(ArrayList<? extends Ye> list) {}public static void method2(ArrayList<? super Zi> list) {}
}class Ye {
}class Fu extends Ye {
}class Zi extends Fu {
}
补充;一个<>中可以写多个类型
案例
package exercise;import javax.swing.*;
import javax.swing.plaf.synth.SynthTextAreaUI;
import javax.swing.text.AbstractDocument;
import java.util.ArrayList;
import java.util.List;public class Demo2 {public static void main(String[] args) {ArrayList<ChineseCat> list1 = new ArrayList<>();ArrayList<PersianCat> list2 = new ArrayList<>();ArrayList<HuskyDog> list3 = new ArrayList<>();ArrayList<Teddy> list4 = new ArrayList<>();keepPet1(list1);keepPet1(list2);keepPet2(list3);keepPet2(list4);}public static void keepPet1(ArrayList<? extends Cat> list) {}public static void keepPet2(ArrayList<? extends Dog> list) {}public static void keepPet3(ArrayList<? extends Animal> list) {}
}abstract class Animal {private String name;private int age;public abstract void eat();public Animal() {}public Animal(String name, int age) {this.name = name;this.age = age;}/*** 获取** @return name*/public String getName() {return name;}/*** 设置** @param name*/public void setName(String name) {this.name = name;}/*** 获取** @return age*/public int getAge() {return age;}/*** 设置** @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Animal{name = " + name + ", age = " + age + "}";}
}abstract class Cat extends Animal {//1.继承抽象类,重写里面所有的方法。//2.本身cat也是一个抽象的,让Cat的子类在重写方法//此时采取第二种处理方案//因为猫的两个子类中的eat的方法体还是不一样的
}class PersianCat extends Cat {public void eat() {System.out.println("一只叫做" + this.getName() + "的" + this.getAge() + "岁的波斯猫,正在吃小饼干。");}
}class ChineseCat extends Cat {public void eat() {System.out.println("一只叫做" + this.getName() + "的" + this.getAge() + "岁的狸花猫,正在吃鱼。");}
}abstract class Dog extends Animal {
}class Teddy extends Dog {@Overridepublic void eat() {System.out.println("一只叫做" + getName() + "的," + getAge() + "岁的泰迪,正在吃骨头,边吃边蹭");}
}class HuskyDog extends Dog {@Overridepublic void eat() {System.out.println("一只叫做" + getName() + "的," + getAge() + "岁的哈士奇,正在吃骨头,边吃边拆家");}
}