扫码一下
查看教程更方便
在面向对象的编程中,抽象是一个向用户隐藏实现细节的过程,只有功能才会提供给用户。 换句话说,用户将获得关于对象做什么而不是它如何做的信息。
在 java 中,抽象是使用抽象类和接口实现的。
在其声明中包含 abstract 关键字的类称为抽象类。抽象类有如下的几条规则:
下面我们看一个抽象类的示例。 要创建一个抽象类,只需在类声明中的 class 关键字之前加上 abstract 关键字。
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 类
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(); } }
当我们编译上面的类时,会产生如下的错误
employee.java:46: employee is abstract; cannot be instantiated
employee e = new employee("george w.", "houston, tx", 43);
^
1 error
我们可以像普通类一样继承 employee 类的属性,方法。如下所示
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 类的相应的属性和方法,如下所示。
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();
// 类定义的剩余部分
}
将方法声明为抽象有两个后果
注意
- 最终,必须有子类实现抽象方法; 否则,我们将拥有无法实例化的抽象类层次结构。
假设 salary 类继承了 employee 类,那么它应该实现 computepay() 方法,如下所示
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; } // 类定义的剩余部分 }