Java - 抽象

Java - 抽象 首页 / Java入门教程 / Java - 抽象

在面向对象的编程中,抽象是对用户隐藏实现细节的过程,只有功能将被提供给用户,在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类的属性,就像具体的类一样-

链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-abstraction.html

来源:LearnFk无涯教程网

/* 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
}

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Flutter核心技术与实战 -〔陈航〕

Netty源码剖析与实战 -〔傅健〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

全链路压测实战30讲 -〔高楼〕

超级访谈:对话张雪峰 -〔张雪峰〕

JavaScript进阶实战课 -〔石川〕

现代C++20实战高手课 -〔卢誉声〕

AI大模型之美 -〔徐文浩〕

结构沟通力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)
1 篇笔记
sxf 2022年03月01日 21:08

1 抽象类不能直接new 

2 抽象方法 需要实现