Java容器-泛型 +练习

泛型
▪ 为什么需要泛型
▪ 解决数据类型操作不统一产生的异常
▪ 使用泛型可以更好的去保护数据类型
▪ 泛型类的定义

泛型类的定义

 使用泛型集合解决实际问题
▪ 声明员工类Employee包含如下属性:id,name,age,gender(枚举类型)
▪ 声明程序员类SE,含有属性popularity人气值
▪ 声明项目经理类PM,含有属性workOfYear工作年限
▪ 程序员和项目经理都继承自Employee
▪ 需求说明:
▪ 使用泛型集合ArrayList,LinkedList,HashSet,TreeSet完成员工的添加,
删除,
▪ 判断,集合中元素个数的判断

员工类Employee

package zyaxu;

import java.util.Objects;

public class Employee{
    String id;
    String name;
    int age;
    String gender;
        zyaxu.EmployeeInformation ID= zyaxu.EmployeeInformation.ID;
        zyaxu.EmployeeInformation NAME= zyaxu.EmployeeInformation.NAME;
        zyaxu.EmployeeInformation AGE= zyaxu.EmployeeInformation.AGE;
        zyaxu.EmployeeInformation GENDER= zyaxu.EmployeeInformation.GENDER;
    public Employee(){}
    public Employee(zyaxu.EmployeeInformation ID, zyaxu.EmployeeInformation NAME, zyaxu.EmployeeInformation AGE,zyaxu.EmployeeInformation GENDER) {
        this.ID = ID;
        this.NAME = NAME;
        this.AGE = AGE;
        this.GENDER = GENDER;
    }

    public Employee(String id, String name, int age, String gender) {
        this.id=id;
        this.name=name;
        this.age=age;
        this.gender=gender;
    }



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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 String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age &&
                Objects.equals(id, employee.id) &&
                Objects.equals(name, employee.name) &&
                Objects.equals(gender, employee.gender);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, gender);
    }
}

程序员类SE

package zyaxu;

public class SE extends Employee{
    private int popularity;
        public SE(EmployeeInformation ID,  EmployeeInformation NAME, EmployeeInformation AGE, EmployeeInformation GENDER, int popularity){
        super(ID,NAME,AGE,GENDER);
        this.popularity=popularity;
    }

    public int getPopularity() {
        return popularity;
    }

    public void setPopularity(int popularity) {
        this.popularity = popularity;
    }
}

项目经理类PM

package zyaxu;

public class PM {
    private int workOfYear;
    public PM(int workOfYear){
        this.workOfYear=workOfYear;
    }

    public int getWorkOfYear() {
        return workOfYear;
    }

    public void setWorkOfYear(int workOfYear) {
        this.workOfYear = workOfYear;
    }
}

SE1 

package Work;

import java.util.Objects;

public class SE extends Employee{
    int popularity;
    public SE(){

    }

    @Override
    public String toString() {
        return "员工信息:{" + "姓名:'" + name + '\'' + ", 性别:'" + gender + '\'' + ", 年龄:" + age + ", 工号:" + id + " 人气值" + popularity+'}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        SE se = (SE) o;
        return popularity == se.popularity;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), popularity);
    }


    public SE(String name, String gender, int age, int id, int popularity) {
        super(name, gender, age, id);
        this.popularity = popularity;
    }

    public int getPopularity() {
        return popularity;
    }

    public void setPopularity(int popularity) {
        this.popularity = popularity;
    }
}

PM1

package Work;

import java.util.Objects;

public class FM extends Employee{

    int workOfYear;
    public FM(){


    }

    @Override
    public String toString() {
        return "经理信息:{" + "姓名:'" + name + '\'' + ", 性别:'" + gender + '\'' + ", 年龄:" + age + ", 工号:" + id + " 工作年龄" +workOfYear+"年" +'}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        FM fm = (FM) o;
        return workOfYear == fm.workOfYear;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), workOfYear);
    }

    public FM(String name, String gender, int age, int id, int workOfYear) {
        super(name, gender, age, id);
        this.workOfYear = workOfYear;

    }

    public int getWorkOfYear() {
        return workOfYear;
    }

    public void setWorkOfYear(int workOfYear) {
        this.workOfYear = workOfYear;
    }
}

TestEmployee

package Work;

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

public class TestEmployee {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        SE se = new SE();
        List list = new ArrayList();
        List PM = new ArrayList();
        PM.add(new FM("王志宇","男",30,100,9));
        PM.add(new FM("赵红鹰","男",35,101,8));
        System.out.println("经理共"+PM.size()+"人,如下:");
        for (Object o : PM){
            System.out.println(o);
        }
        list.add(new SE("小明", "男", 20, 10086, 100));
        list.add(new SE("小黑", "男", 23, 10087, 100));
        list.add(new SE("小白", "女", 21, 10088, 100));
        list.add(new SE("小绿", "女", 22, 10089, 100));
        list.add(new SE("小红", "女", 20, 10090, 100));
        list.add(new SE("小刚", "男", 20, 10091, 100));
        list.add(new SE("小李", "男", 20, 10092, 100));
        System.out.println("员工共"+list.size()+"人,如下:");
        for (Object e : list) {
            System.out.println(e);
        }
        System.out.println("请输入对员工数据库的操作,\"C\"增加,\"D\"删除");
        String s = sc.next();
        if ("c".equals(s) || "C".equals(s)) {
            System.out.println("员工添加系统:");
            System.out.println("请输入要添加员工的姓名:");
            se.name = sc.next();
            System.out.println("请输入要添加员工的性别:");
            se.gender = sc.next();
            System.out.println("请输入要添加员工的年龄:");
            se.age = sc.nextInt();
            System.out.println("请输入要添加员工的工号:");
            se.id = sc.nextInt();
            System.out.println("请输入要添加员工的人气:");
            se.popularity = sc.nextInt();
            list.add(new SE(se.getName(), se.getGender(), se.getAge(), se.getId(), se.getPopularity()));
            System.out.println("添加后员工数据库,人数共" + list.size() + "人");
            for (Object e : list) {
                System.out.println(e);
            }
        } else if ("d".equals(s) || "D".equals(s)) {
            System.out.println("员工删除系统:");
            System.out.println("请输入要删除员工的姓名:");
            se.name = sc.next();
            System.out.println("请输入要删除员工的性别:");
            se.gender = sc.next();
            System.out.println("请输入要删除员工的年龄:");
            se.age = sc.nextInt();
            System.out.println("请输入要删除员工的工号:");
            se.id = sc.nextInt();
            System.out.println("请输入要删除员工的人气:");
            se.popularity = sc.nextInt();
            if (list.contains(new SE(se.getName(), se.getGender(), se.getAge(), se.getId(), se.getPopularity()))) {
                list.remove(new SE(se.getName(), se.getGender(), se.getAge(), se.getId(), se.getPopularity()));
            }
            System.out.println("删除后员工数据库,人数共" + list.size() + "人");
            for (Object e : list) {
                System.out.println(e);
            }
        }else{
            System.out.println("操作指令有误请重新输入");
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值