泛型的使用

泛型的说明

泛(广泛)型(类型)

1)泛型又称参数化类型,是JDK5.0出现的新特性,解决数据类型的安全性问题

2)在类声明或实例化时只要指定好需要的具体的类型即可

3)JAVA泛型可以保证如果程序在编译时没有发出警告,运行时就不会产生

4)泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,或者是某个方法的返回值的类型,

或者是参数类型

public static void main(String[] args) {
​
    Person<String> city01 = new Person<String>("成都");
    Person<Integer> city02 = new Person<Integer>(100);
    city01.play();
    city02.play();
​
}
/*泛型的作用:
* 1.可以在类声明时通过一个标识表示类中某个属性的类型;
* 2.同时也可以是某个方法的返回值的类型,或者是参数类型
* */
​
class Person <E>{//*E表示h的数据类型,该数据类型在定义Person对象时候指定;在编译期间,就确定E的类型
    E h;
​
    public Person(E h) {//E也可以是参数类型
        this.h = h;
    }
​
    public E fun(){//E也可以作为返回值
        return h;
    }
​
    public void play(){
        System.out.println(h.getClass());
    }
​
}

泛型的语法

泛型的声明

interface接口<T>{}和class类<K,V>{}

//例如:List,ArrayList

说明:

1)其中;T,K,V不代表值,而是表示类型;并且任意字母都可以是

泛型的实例化

要在类名后面指定类型参数的值(类型),

例如:

1)List<String> strlist = new ArrayList <String>();

2). Iterator <Customer> iterator = customer.iterator();

练习:

public static void main(String[] args) {
​
    //给泛型指定具体类型后,可以传入该类型或者其子类类型
    Animal<Cat> catAnimal = new Animal<Cat>(new Cat());
    catAnimal.what();
    Animal<Cat> catAnimal1 = new Animal<Cat>(new Tiger());
    catAnimal1.what();
​
}
class Cat{};
class Tiger extends Cat{};
​
class Animal<E>{
    E h;
​
    public Animal(E h) {
        this.h = h;
    }
​
    public void what(){
        System.out.println("Animal的类型:" + h.getClass());
    }
}

泛型语法和使用

泛型使用的注意事项和细节

1.interface List<T>{} , public class HashSet<E>{}...等等

说明:T,E只能是引用类型

List<Integer> list = new ArrayList<Integer>(); //OK

•List<int> list2 = new ArrayList<int>();//错误

2.在给泛型指定具体类型后,可以传入该类型或者其子类类型

3.泛型使用形式

List<lnteger> listl = new ArrayList<Integer>();

List<lnteger> list2 = new ArrayList<>();

如果:List list3 = new ArrayList(); 默认给它的泛型是<E> E 就是 Object

public static void main(String[] args) {
​
    //给泛型指定具体类型后,可以传入该类型或者其子类类型
    Animal<Cat> catAnimal = new Animal<Cat>(new Cat());
    catAnimal.what();
    Animal<Cat> catAnimal1 = new Animal<Cat>(new Tiger());
    catAnimal1.what();
​
}
class Cat{};
class Tiger extends Cat{};
​
class Animal<E>{
    E h;
​
    public Animal(E h) {
        this.h = h;
    }
​
    public void what(){
        System.out.println("Animal的类型:" + h.getClass());
    }
}

泛型的练习 定义Employee类 1)该类包含: private成员变量name.sal,birthday,其中 birthday 为 MyDate类的对象; 2)为每一个属性定义 getter, setter 方法; 3)重写toString 方法输出name, sal, birthday .4) MyDate类包含:private成员变量month,day,year;并为每一个属性定义 getter,.setter方法; 5)创建该类的3个对象,并把这些对象放入ArrayList 集合中(ArrayList需使用泛型来定义),对集合中的元素进行排序,并遍历输出: 排序方式:调用ArrayList 的sort方法,传入Comparator对象[使用泛型],先按照name排序,如果name相同,则按生日日期的先后排序。【即:定制排序】

public static void main(String[] args) {
​
    ArrayList<Employee> employees = new ArrayList<>();
    //定义三个对象
    employees.add(new Employee("jack",7000.80,
                            new MyDate(12,25,2015)));
    employees.add(new Employee("jack",8000.80,
            new MyDate(6,12,2015)));
    employees.add(new Employee("hmith",7500.80,
            new MyDate(9,20,2016)));
​
    //进行进行名字比较;在名字一样时进行年月日的比较
    employees.sort(new Comparator<Employee>() {
        @Override
        public int compare(Employee o1, Employee o2) {
            //判断比较的两个数是否类型是Employee类型
            if(!(o1 instanceof Employee && o2 instanceof Employee)){
                System.out.println("类型不匹配...");
                return 0;
            }
            //比较名字的大小(比较的方法是自然比较法,[value方法中用的字符比较])
            int name = o1.getName().compareTo(o2.getName());
            if(name != 0){
                return name;
            }
​
            //返回年月日比较
            return o1.getBirthday().compareTo(o2.getBirthday());
​
        }
    });
    //遍历ArrayList集合
    System.out.println("employees:" + employees);
​
}
//定义员工类
class Employee{
    private String name;
    private double sal;
    private  MyDate birthday;
​
    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public double getSal() {
        return sal;
    }
​
    public void setSal(double sal) {
        this.sal = sal;
    }
​
    public MyDate getBirthday() {
        return birthday;
    }
​
    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }
​
    @Override
    public String toString() {
        return "\n" + name  + "\t" + sal +"\t" + birthday;
    }
}
​
//定义日月年;并实现Comparable接口
class MyDate implements Comparable<MyDate>{
    private int month;
    private int day;
    private int year;
​
    public MyDate(int month, int day, int year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }
​
    public int getMonth() {
        return month;
    }
​
    public void setMonth(int month) {
        this.month = month;
    }
​
    public int getDay() {
        return day;
    }
​
    public void setDay(int day) {
        this.day = day;
    }
​
    public int getYear() {
        return year;
    }
​
    public void setYear(int year) {
        this.year = year;
    }
​
    @Override
    public String toString() {
        return month + "-" + day + "-"+ year;
    }
​
    //对compareTo的重写
    @Override
    public int compareTo(MyDate date) {
        //比较年份
        int years = this.year - date.getYear();
        if (years != 0){
            return years;
        }
        //比较月份
        int months = this.month - date.getMonth();
        if (months != 0){
            return months;
        }
        //比较天
        int days = 0;
        return  days = this.day - date.getDay();
​
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhan;

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值