Java 抽象类
abstract 关键字、抽象类的规则、继承抽象类与模板方法模式。
抽象类
abstract class is an incomplete class defining a skeleton and leaving details to subclasses. It cannot be instantiated, but you can declare references and extend it.
abstract class Employee {
String name;
Employee(String name) {
this.name = name;
}
abstract double salary();
void printInfo() {
System.out.println(name + " earns " + salary());
}
}
class FullTime extends Employee {
FullTime(String name) {
super(name);
}
@Override
double salary() {
return 8000;
}
}
public class AbstractDemo {
public static void main(String[] args) {
Employee e = new FullTime("Tom");
e.printInfo();
}
}Tom earns 8000.0抽象方法
An abstract method has a declaration only, ending with a semicolon. Subclasses must implement every abstract method or be declared abstract themselves.
abstract class Shape {
abstract double area();
abstract String name();
}
class Rect extends Shape {
double w = 2, h = 3;
@Override
double area() {
return w * h;
}
@Override
String name() {
return "rect";
}
}
public class AbsMethod {
public static void main(String[] args) {
Rect r = new Rect();
System.out.println(r.name() + " " + r.area());
}
}rect 6.0继承抽象类
A subclass extends with extends and still calls the parent constructor first. Abstract classes may mark methods final to lock behaviour while leaving others open for customisation.
abstract class Template {
final void run() {
step1();
step2();
}
void step1() {
System.out.println("step1 default");
}
abstract void step2();
}
class Impl extends Template {
@Override
void step2() {
System.out.println("step2 custom");
}
}
public class TemplateDemo {
public static void main(String[] args) {
new Impl().run();
}
}step1 default
step2 custom抽象类与接口的区别
An abstract class is is-a, single inheritance, may hold state and constructors. An interface is can-do, multiply implementable, with implicitly public static final fields. Share state with abstract classes, share capability with interfaces.
| Aspect | Abstract class | Interface |
|---|---|---|
| Keyword | extends | implements |
| How many | One | Many |
| State | Instance fields allowed | Constants only |
| Constructor | Yes | No |
| Methods | Abstract or concrete | Abstract, default, static |
抽象类
abstract class 表示不完整的类,用来定义「骨架」而把细节留给子类。它不能 new,但可以声明引用、可以被子类继承。
abstract class Employee {
String name;
Employee(String name) {
this.name = name;
}
abstract double salary();
void printInfo() {
System.out.println(name + " earns " + salary());
}
}
class FullTime extends Employee {
FullTime(String name) {
super(name);
}
@Override
double salary() {
return 8000;
}
}
public class AbstractDemo {
public static void main(String[] args) {
Employee e = new FullTime("Tom");
e.printInfo();
}
}Tom earns 8000.0抽象方法
abstract 方法只有声明、没有方法体,以分号结尾。子类必须实现所有抽象方法,否则子类也得声明为 abstract。
abstract class Shape {
abstract double area();
abstract String name();
}
class Rect extends Shape {
double w = 2, h = 3;
@Override
double area() {
return w * h;
}
@Override
String name() {
return "rect";
}
}
public class AbsMethod {
public static void main(String[] args) {
Rect r = new Rect();
System.out.println(r.name() + " " + r.area());
}
}rect 6.0继承抽象类
子类用 extends 继承抽象类,构造时同样先调父类构造。抽象类里可以有 final 方法锁定行为,只开放部分方法给子类定制。
abstract class Template {
final void run() {
step1();
step2();
}
void step1() {
System.out.println("step1 default");
}
abstract void step2();
}
class Impl extends Template {
@Override
void step2() {
System.out.println("step2 custom");
}
}
public class TemplateDemo {
public static void main(String[] args) {
new Impl().run();
}
}step1 default
step2 custom抽象类与接口的区别
抽象类是「is-a」,只能继承一个,可以有状态和构造方法;接口是「can-do」,可以实现多个,字段隐式为 public static final。需要共享状态用抽象类,需要多重能力用接口。
| 对比项 | 抽象类 | 接口 |
|---|---|---|
| 关键字 | extends | implements |
| 数量 | 一个 | 多个 |
| 状态 | 可有实例字段 | 只能有常量 |
| 构造方法 | 有 | 没有 |
| 方法 | 抽象或具体 | 抽象、默认、静态 |