Day007-2021-08-06 构造函数 继承/多态 重载/重写 接口与匿名内部类 迭代器/比较器

第七天,面向对象一些更深的知识

一、构造函数

构造函数是一个类创建对象的方法。一个类在创建成功以后,会有一个默认的隐藏的无参构造函数。如果根据需要,在创建对象的过程中,需要赋给某些属性数值,可以手动添加一个有参构造。在添加了有参构造方法后,那个默认的无参构造函数就会消失。有参构造的格式:

public 类名(对应数据类型 属性1, 对应数据类型 属性2, ...) {
	super();
	this.属性1 = 属性1;
	this.属性2 = 属性2;
	...
}

二、继承

一个类可以继承另一个类,来继承这个类的全部公共的成员。

2.1 子父类关系

一个类继承了另一个类,那么这个类叫做子类,也可以叫作派生类,被继承的类就是它的父类,也可以叫作基类或超类。
每个类只能继承一个类,但是可以被很多类继承。
以生物学来说,动物和植物继承了生物,而猫、狗、狮子、老虎又都继承了动物。继承的格式:

public class 子类 extends 父类 {
}

2.2 创建对象

创建对象时,父类的引用可以指向子类的对象,其格式:

父类 变量名 = new 子类();

创建出来的对象,方法名列表看父类,执行过程看子类。

三、重载/重写

在添加了一个构造方法后,如果还需要之前的无参构造,可以再手动添加一个无参构造。这两个构造方法形成了方法重载。重载指的是两个方法,函数名相同,参数的个数,顺序,类型不同。函数重载与返回值无关。
在一个类继承了另一个类后,重新写了父类中的一个方法,这个过程叫做重写。常见的方法重写有 toString() 方法。方法重写通常用 @Override 标识。

四、多态

多态的条件:

  1. 几个子类继承了同一个父类。
  2. 这几个子类重写了父类中的同一个方法。
  3. 父类的引用指向了子类的对象。
父类 a1 = new 子类1();
父类 a2 = new 子类2();
父类 a3 = new 子类3();
a1.方法();
a2.方法();
a3.方法();

a1、a2、a3是三个不同子类的对象,这三个子类都重写了同一个方法,那么这三句执行的结果看各自子类重写的结果。这个现象就叫做多态。

五、接口与匿名内部类

接口(interface)与类(class)相似,只不过接口中会有抽象方法。实现接口的过程与类的继承也十分相似。其格式:

public classimplements 接口 {
}

接口一般不能实例化,如果有实例化的需要,则应当先重写补全所有的抽象方法。当实例只在一个类里面需要时,可以通过匿名内部类的方式来创建。其格式:

接口 实例对象名 = new 接口(){
//补全抽象方法
@Override
方法重写的具体内容;
};

六、迭代器

6.1 为什么要迭代器

迭代器是遍历集合的一种方法。在遍历集合的过程中,用 remove() 方法删除一个元素比较麻烦,而使用迭代器删除相对简单。

6.2 迭代器的应用

迭代器的格式:

Iterator<类名> 迭代器名 = 集合.iterator();
while (迭代器名.hasNext()) {
	if (删除的条件) {
		迭代器名.remove();
	}
}

七、比较器

7.1 为什么要比较器

常规的排序算法只针对能比较大小的基本类型,而现实业务需要对各种类型进行排序,而且在很多情况下都不是单一的排序规则。

7.2 比较器的应用

比较器的格式:

Comparator<类名> 比较器名 = new Comparator<类名>() {
	@Override
	public int compare(类名 o1, 类名 o2) {
		return 根据需要返回的值;
	}
};
Collections.sort(集合名, 比较器名);

八、练习

创建一个员工类(Employee),伪造一份员工数据(FakeData),搭建一个员工管理系统(EmployeeManagement),封装主要的操作方法(EmployeeDao)。

public class Employee {
	public int id;
	public String name;
	public String sex;
	public int age;
	public int salary;

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

	@Override
	public String toString() {
		return id + "号" + sex + "员工 " + name + " , " + age + "岁月薪" + salary;
	}

}
import java.util.ArrayList;

public class FakeData {
	public static ArrayList<Employee> employeeList=new ArrayList<Employee>();
	
	static {
		employeeList.add(new Employee(1, "张老狗", "男", 35, 3000));
		employeeList.add(new Employee(2, "王大炮", "男", 28, 6000));
		employeeList.add(new Employee(3, "李狗蛋", "男", 24, 12000));
		employeeList.add(new Employee(4, "赵二蛋", "女", 19, 30000));
		employeeList.add(new Employee(5, "孙建国", "女", 26, 9000));
	}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

public class EmployeeDao {

	public static void showEmployeeList(ArrayList<Employee> list) {
		list.forEach(System.out::println);
	}

	public static Integer stringToInt(String str) {
		Integer id = null;
		try {
			id = Integer.parseInt(str);
		} catch (Exception e) {
			return null;
		}
		return id;
	}

	public static Employee searchEmployeeById(int id, ArrayList<Employee> list) {
		for (Employee emp : list) {
			if (emp.id == id) {
				return emp;
			}
		}
		return null;
	}

	public static void removeEmployeeById(int id, ArrayList<Employee> list) {
		Iterator<Employee> ite = list.iterator();
		while (ite.hasNext()) {
			if (ite.next().id == id) {
				ite.remove();
			}
		}
	}

	public static void sortEmployee(String str, boolean flag, ArrayList<Employee> list) {
		if ("id".equals(str)) {
			Comparator<Employee> com = new Comparator<Employee>() {
				@Override
				public int compare(Employee o1, Employee o2) {
					return (o1.id - o2.id) * (flag ? 1 : -1);
				}
			};
			Collections.sort(list, com);
		} else if ("age".equals(str)) {
			Comparator<Employee> com = new Comparator<Employee>() {
				@Override
				public int compare(Employee o1, Employee o2) {
					return (o1.age - o2.age) * (flag ? 1 : -1);
				}
			};
			Collections.sort(list, com);
		} else if ("salary".equals(str)) {
			Comparator<Employee> com = new Comparator<Employee>() {
				@Override
				public int compare(Employee o1, Employee o2) {
					return (o1.salary - o2.salary) * (flag ? 1 : -1);
				}
			};
			Collections.sort(list, com);
		}
	}
}
import java.util.Scanner;

public class EmployeeManagement {
	private static Scanner scan = new Scanner(System.in);

	public static void main(String[] args) {
		System.out.println("---welcome employee management---");
		while (true) {
			System.out.println("---main menu---");
			System.out.println("1:show all employee list");
			System.out.println("2:search employee by id");
			System.out.println("3:remove employee by id");
			System.out.println("4:sort employee by keyword");
			System.out.println("-------------------");
			String in = scan.next();
			if ("1".equals(in)) {
				System.out.println("---employee list---");
				EmployeeDao.showEmployeeList(FakeData.employeeList);
				System.out.println("----------------------");
			} else if ("2".equals(in)) {
				System.out.println("please provide an id");
				String in2 = scan.next();
				Integer id = EmployeeDao.stringToInt(in2);
				if (id == null) {
					System.out.println("illegal id !");
					System.out.println("----------------------");
					continue;
				}
				Employee emp = EmployeeDao.searchEmployeeById(id, FakeData.employeeList);
				if (emp == null) {
					System.out.println("not found !");
					System.out.println("----------------------");
				} else {
					System.out.println("---search result---");
					System.out.println(emp);
					System.out.println("---------------------");
				}
			} else if ("3".equals(in)) {
				System.out.println("please provide an id");
				String in2 = scan.next();
				Integer id = EmployeeDao.stringToInt(in2);
				if (id == null) {
					System.out.println("illegal id !");
					System.out.println("----------------------");
					continue;
				}
				EmployeeDao.removeEmployeeById(id, FakeData.employeeList);
				System.out.println("remove success");
				System.out.println("----------------------");
			} else if ("4".equals(in)) {
				System.out.println("please provide id or age or salary");
				String in2 = scan.next();
				if (!("id".equals(in2) || "age".equals(in2) || "salary".equals(in2))) {
					System.out.println("illegal keyword");
					System.out.println("----------------------");
					continue;
				}
				System.out.println("do you need asc or desc");
				String in3 = scan.next();
				boolean flag = false;
				if ("asc".equals(in3)) {
					flag = true;
				} else if ("desc".equals(in3)) {
					flag = false;
				} else {
					System.out.println("illegal choise");
					System.out.println("----------------------");
					continue;
				}
				EmployeeDao.sortEmployee(in2, flag, FakeData.employeeList);
				System.out.println("---employee list---");
				EmployeeDao.showEmployeeList(FakeData.employeeList);
				System.out.println("----------------------");
			}
		}
	}
}

运行结果:
运行结果

static {
}

这句是静态代码块,会在整个程序执行前加载好内容,且只会运行一次。

list.forEach(System.out::println);

这句是 Lambda 表达式的缩写。补全后:

list.forEach(x -> {
	System.out.println(x);
});
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值