泛型语法
  TEZNKK3IfmPf 2023年11月13日 26 0

2. 泛型使用的注意事项和细节  557-558

1. interface List{} , public class HashSet {}..等等说明: T, E只能是引用类型

看看下面语句是否正确?:

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

List list2 = new ArrayList < int> 0;//错误

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

3. 泛型使用形式

List< Integer> list1 = new ArrayList  0;

List< Integer> list2 = new ArrayList< > 0; [说明:]

3. 如果我们这样写List list3 = new ArrayList(); 默认给它的泛型是[ E就是Object ]

代码在com.stulzl.generic_detail.包中

Generic_Detail

package com.stulzl.generic_detail;

import java.util.ArrayList;
import java.util.List;

//泛型的细节  557-558
public class Generic_Detail {
    public static void main(String[] args) {
        //interface List{} , public class HashSet {}..等等说明: T, E只能是引用类型
        //1.给泛型指向数据类型是,要求是引用类型,不能是基本数据类型
        List list = new ArrayList(); //OK
        //List list2 = new ArrayList();//错误

        //2.在给泛型指定具体类型后,可以传入该类型或者其子类类型
        // 说明
        //因为 E 指定了 A 类型, 构造器传入了 new A()
        Pig aPig = new Pig(new A());//这里项相当于把A类当作了Pig的泛型
        aPig.show();//class com.stulzl.generic_detail.A
        Pig aPig2 = new Pig(new B());//因为B是A的子类,也可传入
        aPig2.show();//class com.stulzl.generic_detail.B

        //3.泛型的使用形式
        List list1 = new ArrayList();
        ArrayList list2 = new ArrayList();
        //在实际开发中,我们往往简写
        //编译器会进行类型推断, 老师推荐使用下面写法
        List list3 = new ArrayList<>();
        ArrayList list4 = new ArrayList<>();

        //4. 如果是这样写 泛型默认是 Object
        //等价 ArrayList arrayList = new ArrayList();
        ArrayList arrayList = new ArrayList();

        Tiger tiger = new Tiger();
        /*
            相当于
            class Tiger {//类
                Object e;

                public Tiger() {}

                public Tiger(Object e) {
                    this.e = e;
                }
            }

         */

    }
}
class Tiger{
    E e;
    public Tiger() {
    }

    public Tiger(E e) {
        this.e = e;
    }
}
class A{

}
class B extends A{

}
class Pig{
    E e;

    public Pig(E e) {
        this.e = e;
    }

    public void show(){
        System.out.println(e.getClass());//返回e的类型
    }
}

3. 泛型练习题  559

定义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相同,则按生日日期的先后排序。 (即:定制排序)

代码在com.stulzl.generic_exercise02.包中

GenericExercise02

package com.stulzl.generic_exercise02;

import java.util.ArrayList;
import java.util.Comparator;

//泛型练习  559
//定义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相同,则按生日日期的先 后排序。 (即:定制排序)
@SuppressWarnings({"all"})
public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList employees = new ArrayList<>();
        employees.add(new Employee("jack",18000,new MyDate(2000,2,22)));
        employees.add(new Employee("tom",2000,new MyDate(2002,5,20)));
        employees.add(new Employee("tom",1600,new MyDate(2002,5,15)));
        //排序
        employees.sort(new Comparator() {
            @Override
            public int compare(Employee o1, Employee o2) {
                //数据验证
                if(!(o1 instanceof Employee && o2 instanceof Employee)){
                    System.out.println("类型不匹配……");
                    return 0;
                }
                //先按照name排序,如果name相同,则按生日日期的先后排序。
                int i = (o1.getName()).compareTo(o2.getName());
                if(i !=0){//名字不同,直接返回
                    return i;
                }
                //代码简化后直接调用MyDate中重写的compareTo方法即可
                return o1.getBirthday().compareTo(o2.getBirthday());

                //下面是对 birthday 的比较,因此,我们最好把这个比较,放在 MyDate 类完成
                //因为这一大串代码看着好冗余,我们写到MyDate类中,简化代码

                //名字相同比较年
                /*int yearMinus = o1.getBirthday().getYear()-o2.getBirthday().getYear();
                if(yearMinus !=0){//年不同
                    return yearMinus;
                }
                //年也相同,比较月
                int monthMinus = o1.getBirthday().getMonth()-o2.getBirthday().getMonth();
                if(monthMinus !=0){//月不同
                    return monthMinus;
                }
                //年月也相同,比较日
                return o1.getBirthday().getDay()-o2.getBirthday().getDay();*/
            }
        });

        System.out.println("=====排序后=====");
        //遍历
        for (Employee o :employees) {
            System.out.println(o);
        }

    }
}
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 "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}
class MyDate implements Comparable{//让MyDate实现Comparable接口实现对MyDate的比较
    private int year;
    private int month;
    private int day;

    public MyDate(int year,int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    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 year+"-"+month +"-"+ day ;

    }

    //MyDate重写compareTo方法,按照题目要求实现比较次序
    @Override
    public int compareTo(MyDate o) {
        //名字相同比较年
        int yearMinus = year-o.getYear();
        if(yearMinus !=0){//年不同
            return yearMinus;
        }
        //年也相同,比较月
        int monthMinus = month-o.getMonth();
        if(monthMinus !=0){//月不同
            return monthMinus;
        }
        //年月也相同,比较日
        return day-o.getDay();
    }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月15日   16   0   0 泛型java
  TEZNKK3IfmPf   2023年11月14日   30   0   0 泛型list
  TEZNKK3IfmPf   2024年04月19日   45   0   0 泛型java
  TEZNKK3IfmPf   2024年03月29日   60   0   0 list
  TEZNKK3IfmPf   2024年03月29日   33   0   0 listjava数组
  TEZNKK3IfmPf   2023年11月14日   51   0   0 listpytorch
TEZNKK3IfmPf