package cn.cast.collection;import java.util.ArrayList;
import java.util.Iterator;/*** @author zhangyu* @date 2021年08月29日 7:43 下午* 泛型:jdk中的泛型为伪泛型* 在编译时期有效,解决安安全问题*/
public class test {public static void main(String[] args) {ArrayList<String> arr = new ArrayList();arr.add("123kahfkadfhkdh");arr.add("234");Iterator<String> it = arr.iterator();while (it.hasNext()){System.out.println(it.next().length());}}}
/eg
public abstract class Company {private String name;private int age;private double salary;public abstract void job();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;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Company(String name, int age, double salary) {this.name = name;this.age = age;this.salary = salary;}
}
/继承
public class Maniger extends Company {private double bonus;public Maniger(String name, int age, double salary, double bouns) {super(name, age, salary);this.bonus = bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}public void job() {System.out.println(getName()+getAge()+getSalary()+getBonus()+"经理");}
}
public class Employee extends Company{public void job() {System.out.println(getName()+getAge()+getSalary()+"程序员");}public Employee(String name, int age, double salary) {super(name, age, salary);}
}
package cn.cast.collection;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;/*** @author zhangyu* @date 2021年09月04日 6:10 下午* f泛型限定:* 1.上限限定 是父类 ? extend E* 2.下限限定 是子类 ? super E*/
public class GenericDome {public static void main(String[] args) {//创建一个集合,存储Manerger对象ArrayList<Maniger> maniger = new ArrayList<>();maniger.add(new Maniger("李四",18,1234.5,456));maniger.add(new Maniger("刘能",23,9807,769));ArrayList<Employee> employee = new ArrayList<>();employee.add(new Employee("谢大脚",12,345));employee.add(new Employee("王长贵",45,809));methon(maniger);methon(employee);}//定义方法,同时遍历两个对象public static void methon(List<? extends Company> list){Iterator<? extends Company> it = list.iterator();while (it.hasNext()){Company c = it.next();c.job();}}
}