Java-抽象


根据字典,抽象是处理想法而不是事件的质量。例如,当您考虑电子邮件的情况时,复杂的细节(例如发送电子邮件后会发生什么)、电子邮件服务器使用的协议对用户是隐藏的。因此,要发送电子邮件,您只需键入内容,提及收件人地址,然后单击“发送”。

同样,在面向对象编程中,抽象是向用户隐藏实现细节的过程,仅向用户提供功能。换句话说,用户将获得有关对象做什么的信息,而不是它如何做的信息。

在Java中,抽象是通过抽象类和接口来实现的。

抽象类

在声明中包含abstract关键字的类称为抽象类。

  • 抽象类可能包含也可能不包含抽象方法,即没有主体的方法( public void get(); )

  • 但是,如果一个类至少有一个抽象方法,则该类必须声明为抽象的。

  • 如果一个类被声明为抽象类,则它不能被实例化。

  • 要使用抽象类,您必须从另一个类继承它,并提供其中抽象方法的实现。

  • 如果继承抽象类,则必须提供其中所有抽象方法的实现。

例子

本节为您提供抽象类的示例。要创建抽象类,只需在类声明中的 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 中的普通类相同。该类现在是抽象的,但它仍然具有 3 个字段、7 个方法和 1 个构造函数。

现在您可以尝试通过以下方式实例化 Employee 类 -

/* File name : AbstractDemo.java */
public class AbstractDemo {

   public static void main(String [] args) {
      /* Following is not allowed and would raise error */
      Employee e = new Employee("George W.", "Houston, TX", 43);
      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
   }
}

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: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;   // Annual 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 类,并且使用该实例您可以访问 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();
   }
}
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;
   }
}
class Salary extends Employee {
   private double salary;   // Annual 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;
   }
}

输出

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

抽象方法

如果您希望一个类包含特定方法,但希望该方法的实际实现由子类确定,则可以在父类中将该方法声明为抽象方法。

  • Abstract关键字用于将方法声明为抽象方法。

  • 您必须将抽象关键字放在方法声明中的方法名称之前。

  • 抽象方法包含方法签名,但不包含方法体。

  • 抽象方法末尾将有一个符号冒号 (;),而不是花括号。

以下是抽象方法的示例。

例子

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
}

例子

以下示例展示了抽象方法的概念。

/* File name : AbstractDemo.java */
public class AbstractDemo {
   public static void main(String [] args) {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
      System.out.println("salary: " + s.computePay());
   }
}
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 abstract double computePay();
      // Remainder of class definition

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getAddress() {
      return address;
   }

   public void setAddress(String address) {
      this.address = address;
   }

   public int getNumber() {
      return number;
   }

   public void setNumber(int number) {
      this.number = number;
   }
}
class Salary extends Employee {
   private double salary;   // Annual salary

   public Salary(String name, String address, int number, double salary) {
      super(name, address, number);
      this.salary = salary;
   }

   public double computePay() {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }
   // Remainder of class definition
}

输出

Constructing an Employee
Computing salary pay for Mohd Mohtashim
salary: 69.23076923076923