前言:嘿嘿,昨天下午和高中的几个hxd一起吃吃饭玩玩剧本杀,好开心
ps:古木吟这个剧本杀剧本真好玩。
玩之前:我们都是很聪明的玩家,虽然是第一次玩
玩之后:240分钟的古木吟我们玩了300多分钟,我是fw~(自豪)
1.什么是抽象概念?抽象概念的意义?
抽象就是在设计中,由于子类对象对父类对象是全包含的,所以我们希望父类中含有通用的方法和属性供子类对象使用和重写,而我们只需要根据不同的功能去new不同的子类对象即可,而不用再去new父类对象,让父类对象专心做模板,所以我们引入了抽象概念,即父类提抽象概念(无方法体),new的子类去具体实现(重写abstract抽象方法的子类对应方法体)
抽象在Java'中的体现分为:
1.1抽象方法
正常的一个方法(有具体方法体):
public void eat() {
System.out.println("俺要吃饭~");
}
抽象的方法(没有具体方法体):
public abstract void eat();
1.2抽象类
public abstract class Test1 {
// public void eat() {
// System.out.println("俺要吃饭~");
// }
public abstract void eat();
}
含有抽象方法的一定是一个抽象类
2.抽象的限制与注意事项
2.1抽象对象只能用来修饰类和方法,没办法修饰变量等其他部分
2.2子类继承抽象父类需要对父类中的抽象办法进行具体的重写,如果不进行重写,那子类也必须是抽象的,无法实例化的(直到有一个它的子类重写了抽象办法,让抽象办法在子类对象中不再抽象)。
2.3由于2.2的存在,那我们就必须保证子类能对父类的抽象办法进行重写,这也就导致了abstract不能用于static方法,因为只有非static的方法有重写,static方法没有重写这个说法,子类也就永远无法实例化。
2.4 子类要对父类重写,那就不能用来修饰已经被final关键字修饰的方法,毕竟final是最终了,没办法进行任何修改,也就同2.4了
2.5抽象方法不能设置成private类型的,因为如果设置成private类型,子类就没办法重写方法了。
3.抽象的好处
①更加清晰合理的处理代码
②可以实现模板格式
ps:什么是模板格式?
a:举例“人去银行办业务”
都要实现以下几步:1.取号等待 2.具体业务 3.意见反馈
那么一般来说 1~3是都通用的流程(父类直接定义通用的),只是到2的时候每个人都有自己独特的需求(父类只给个abstract,具体由不同子类来实现不同功能)
在代码上就能更加简洁和美化
一般来说我们把这个2过程称为挂钩(应该挺形象的hhhh)
4.抽象的实战
4.1题目
4.2具体代码
4.2.1 抽象的父类employee
package AbstractTest;
abstract public class Employee {
private String name;
private String id;
private MyDate birthday;
public Employee() {
super();
}
public Employee(String name, String id, MyDate birthday) {
super();
this.name = name;
this.id = id;
this.birthday = birthday;
}
@Override
public String toString() {
return "name=" + name + ", id=" + id + ", birthday=" + birthday.toDateString() ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
abstract public double earnings();
}
4.2.2 工具类型MyDate
package AbstractTest;
public class MyDate {
private int year;
private int month;
private int day;
public String toDateString() {
return year + "年" + month + "月" + day + "日";
}
public int getYear() {
return year;
}
public void setYear(int year) {
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 MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
}
4.2.3 子类1:SalariedEmployee
package AbstractTest;
public class SalariedEmployee extends Employee {
private double monthlySalary;
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public SalariedEmployee(String name, String id, MyDate birthday) {
super(name, id, birthday);
// TODO Auto-generated constructor stub
}
public SalariedEmployee(String name, String id, MyDate birthday,double monthlySalary) {
super(name, id, birthday);
// TODO Auto-generated constructor stub
this.monthlySalary = monthlySalary;
}
@Override
public double earnings() {
return monthlySalary;
}
@Override
public String toString() {
//solution1
// return "SalariedEmployee [name = " + getName() + "id =" + getId() + "birthday ="
// + getBirthday().toDateString()/* +"monthlySalary=" + monthlySalary */+ "]";
//solution2
return "SalariedEmployee [" +super.toString() +"]";
}
}
4.2.4 子类2:HourlyEmployee
package AbstractTest;
public class HourlyEmployee extends Employee{
private double wage;
private int hour;
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public HourlyEmployee(String name, String id, MyDate birthday,double wage,int hour) {
super(name, id, birthday);
// TODO Auto-generated constructor stub
this.wage = wage;
this.hour = hour;
}
@Override
public double earnings() {
// TODO Auto-generated method stub
return wage * hour;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "HourlyEmployee["+super.toString()+"]";
}
}
4.2.5 最后的测试类 PayrollSystem
package AbstractTest;
import java.sql.Date;
import java.util.Calendar;
public class PayrollSystem {
public static void main(String[] args) {
Employee[] employees = new Employee[10];
// for(int i = 0;i<employees.length;i++) {
// employees[i] = new HourlyEmployee();
// }
employees[0] = new SalariedEmployee("max","1001",new MyDate(2000,10,21),12000);
employees[1] = new SalariedEmployee("min","1002",new MyDate(2000,11,21),12000);
employees[2] = new HourlyEmployee("minus","1003",new MyDate(2000,2,21),120,100);
PayrollSystem p = new PayrollSystem();
p.getEarns(employees[0]);
p.getEarns(employees[1]);
p.getEarns(employees[2]);
}
public void getEarns(Employee e) {
//获取当前月份
Calendar instance = Calendar.getInstance();
int curMonth = instance.get(Calendar.MONTH);
System.out.println(e);
if(e.getBirthday().getMonth() == (curMonth-1)) {
System.out.println("您的当月工资为:"+(e.earnings()+100)+",生日快乐!");
}else{
System.out.println("您的当月工资为:"+e.earnings());
}
}
}