抽象类 抽象方法(abstract)
abstract可以用来修饰的结构:类,方法;不能用来修饰属性,构造器,私有方法,静态方法,final的方法,final的类
抽象类
一旦一个类被abstract修饰,那么这个类就不能创建对象(实例化)。
开发中都会提供抽象类的子类,让子类对象实例化,完成相关的操作。
抽象方法
抽象广发只有方法的声明,没有方法体
包含抽象方法的类,一定是一个抽象类。反之,抽象类中可以没有抽象方法。
若子类重写了父类中的所有的抽象方法后,子类才可以实例化;若子类没有重写父类中的所有抽象方法,则此子类也是一个抽象类,需要使用abstract修饰
为什么抽象类不可以使用final关键字声明?
这里首先得了解抽象和final,abstract类一旦被定义,那么该类就无法创建对象了,也就是说,这个类的功能要想被实现,只能通过继承来实现。而final定义的类是无法继承的,因此两者之间存在矛盾,故abstract不能喝final一起修饰同一个类。
练习
package com.helloworld.java;
public class HelloWorld {
public static void main(String[] args) {
Employee manager = new Manager("joke", 1001, 2000, 300);
manager.work();
CommonEmployee commonEmployee = new CommonEmployee();
commonEmployee.work();
}
}
abstract class Employee{
private String name;
private int id;
private double salary;
public Employee(){
super();
}
public Employee(String name, int id, double salary){
super();
this.name = name;
this.id = id;
this.salary = salary;
}
public abstract void work();
}
//
class Manager extends Employee{
private double bonus;
public Manager(double bonus){
super();
this.bonus = bonus;
}
public Manager(String name,int id,double salary, double bonus){
super(name, id, salary);
this.bonus = bonus;
}
public void work(){
System.out.println("管理员工,提高公司运行效率");
}
}
class CommonEmployee extends Employee{
public void work(){
System.out.println("普通员工");
}
}
多态的应用-模板方法设计模式
抽象类体现的就是一种模板模式的设计,抽象列作为多个子类的通用模板,子类在抽象类的基础上进行扩展、改造,但子类总体上会保留抽象类的行为方式。
解决的问题:
- 当功能内部一部分实现是确定的,一部分实现是不确定的。这时可以把不确定的部分暴露出去,让子类去实现。
- 在软加开发中实现一个算法时,整体步骤很固定,通用。这些步骤已经在父类中写好了。但是某些部分易变,易变部分可以抽象出来,供不同子类实现。这就是模板模式。
实例:模板方法
package com.helloworld.java;
public class HelloWorld {
public static void main(String[] args) {
Template t = new SubTemplate();
t.dpendTime();
}
}
//计算一段代码的运行时间
abstract class Template{
public void dpendTime(){
long start = System.currentTimeMillis();
code();
long end = System.currentTimeMillis();
System.out.println("the time is " + (end - start) + "ms");
}
public abstract void code();
}
class SubTemplate extends Template{
public void code(){
for(int i = 2;i <= 1000;i++){
boolean isFlag = true;
for(int j = 2; j <=Math.sqrt(i);j++){
if(i % j == 0){
isFlag = false;
break;
}
}
if(isFlag){
System.out.println(i);
}
}
}
}
抽象类练习
题目要求:
- 编写工资系统,实现不同类型员工(多态)按月发放工资。如果当月出现某个Employee对象的生日,则将该雇员的工资增加100元。
- 定义一个Employee类,包含name,number,birthday;birthday为MyDate类对象;abstract方法earnings();toString方法输出对象的name,number,birthday。
- MyDate类包含:private成员变量-year,month,day;toDateString方法返回日期对应的字符串
- 定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。
- 参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。
- 定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用,利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象的生日。当键盘输入本月月份时,如果本月是某个Employee对象的生日,还要输出增加工资的信息。
代碼
package com.helloworld.java;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {//这里的helloworld类就是PayrollSystem类用来进入主程序
Scanner scanner = new Scanner(System.in);
System.out.println("请输入当月的月份:");
int month = scanner.nextInt();
Employee[] emps = new Employee[2];//这里声明是的Employe数组,数组此时存放的是null,故此时没有报错(抽象类是不能创建对象的)
emps[0] = new SalariedEmployee("jok", 1001, new MyDate(1998, 3, 6));//这里是new对象的操作,不能new Empoyee(抽象类)
emps[1] = new HourlyEmployee("hide", 2001, new MyDate(1999, 6, 8));
for(int i = 0;i <emps.length;i++){
System.out.println(emps[i]);
double salary = emps[i].earnings();
System.out.println("月工资为:"+ salary);
if(month == emps[i].getBirthday().getMonth()){
System.out.println("生日快乐,奖励100元!");
}
}
}
}
abstract class Employee{
private String name;
private int number;
private MyDate birthday;
public Employee(String name, int number, MyDate birthday){
super();
this.name = name;
this.number = number;
this.birthday = birthday;
}
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public abstract double earnings();
public String toString(){
return "Employee[name =" + ", number =" + number + ", birthday = " + birthday.toDateString() + "]";
}
}
class MyDate{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day ){
super();
this.year = year;
this.month = month;
this.day = day;
}
public int getMonth() {
return month;
}
public String toDateString(){
return year + "年" + month + "月" + day + "日";
}
}
class SalariedEmployee extends Employee{
private double monthlySalary;
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public SalariedEmployee(String name, int number, MyDate birthday){
super(name , number, birthday);
}
public double earnings(){
return monthlySalary;
}
public String toString(){
return "SalariedEmployee[" + super.toString() + "]";
}
}
class HourlyEmployee extends Employee{
private int wage;//每小时的工资
private int hour;//月工作的小时数
public HourlyEmployee(String name , int number,MyDate birthday){
super(name, number, birthday);
}
public double earnings (){
return 0;
}
public String toString(){
return "HourlyEmployee[" + super.toString() + "]";
}
}