在面向对象的编程中,抽象是对用户隐藏实现细节的过程,只有功能将被提供给用户,在Java中,抽象是使用抽象类和接口实现的。
本节提供了一个抽象类的示例。要创建抽象类,只需在类声明中在class关键字之前使用抽象(abstract)关键字。
/* File name : Employee.java */ public abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; this.address = address; this.number = number; } public double computePay() { System.out.println("Inside Employee computePay"); return 0.0; } public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address); } public String toString() { return name + " " + address + " " + number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address = newAddress; } public int getNumber() { return number; } }
您可以观察到,除了抽象方法外,Employee类与Java中的普通类相同。现在您可以尝试通过以下方式实例化Employee类-
/* File name : AbstractDemo.java */ public class AbstractDemo { public static void main(String [] args) { /* 以下是不允许的,并且会引发错误 */ Employee e = new Employee("George W.", "Houston, TX", 43); System.out.println("\n Call mailCheck using Employee reference--"); e.mailCheck(); } }
当您编译上面的类时,它会给出以下错误-
Employee.java:46: Employee is abstract; cannot be instantiated Employee e=new Employee("George W.", "Houston, TX", 43); ^ 1 error
无涯教程可以通过以下方式继承Employee类的属性,就像具体的类一样-
/* File name : Salary.java */ public class Salary extends Employee { private double salary; //年薪 public Salary(String name, String address, int number, double salary) { super(name, address, number); setSalary(salary); } public void mailCheck() { System.out.println("Within mailCheck of Salary class "); System.out.println("Mailing check to " + getName() + " with salary " + salary); } public double getSalary() { return salary; } public void setSalary(double newSalary) { if(newSalary >= 0.0) { salary = newSalary; } } public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } }
在这里,您不能实例化Employee类,但可以实例化Salary Class,并且使用此实例可以访问Employee类,如下所示。
/* File name : AbstractDemo.java */ public class AbstractDemo { public static void main(String [] args) { Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00); Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00); System.out.println("Call mailCheck using Salary reference --"); s.mailCheck(); System.out.println("\n Call mailCheck using Employee reference--"); e.mailCheck(); } }
这将生成以下输出-
Constructing an Employee Constructing an Employee Call mailCheck using Salary reference -- Within mailCheck of Salary class Mailing check to Mohd Mohtashim with salary 3600.0 Call mailCheck using Employee reference-- Within mailCheck of Salary class Mailing check to John Adams with salary 2400.0
下面是抽象方法的示例。
public abstract class Employee { private String name; private String address; private int number; public abstract double computePay(); //Remainder of class definition }
假设Salary类继承Employee类,那么它应该实现ComputePay()方法,如下所示-
/* File name : Salary.java */ public class Salary extends Employee { private double salary; //Annual salary public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } //Remainder of class definition }
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
1 抽象类不能直接new
2 抽象方法 需要实现