Java中的抽象类
抽象类
在其声明中包含abstract关键字的类称为抽象类。
抽象类可能包含也可能不包含抽象方法,即没有主体的方法(publicvoidget();)
但是,如果一个类至少具有一个抽象方法,则必须将该类声明为抽象。
如果类被声明为抽象,则无法实例化。
要使用抽象类,您必须从另一个类继承它,并为其中的抽象方法提供实现。
如果继承抽象类,则必须为其中的所有抽象方法提供实现。
示例
本节为您提供了抽象类的示例。要创建抽象类,只需在类声明中的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) {
/* Following is not allowed and would raise error */
Employee e = new Employee("乔治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("乔治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();
}
}这产生以下结果-
输出结果
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
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短