OOP17-抽象类和接口

抽象类与继承

应用场景

在编写一个类的方法时, 暂时想不到如何实现这个方法-很可能由于没有足够的信息实现这个方法. 可以把这个方法暂时不实现, 定义为抽象(abstract)方法. 留给从这个类继承(扩充, extends) 出更具体的类时-有足够多的信息时, 实现这个方法.
具有抽象(abstract)方法的类, 由于含有没有实现的函数, 不能用于创建对象. 因此, 把含有抽象方法的类, 称为抽象(abstract)类.

抽象类及其抽象方法

抽象方法, 在方法的说明中, 包含abstract关键词.
抽象类, 在类的说明中, 包含abstract关键词.

应用示例

定义一个账户之间转账不收取转账手续费, 可以定义转账(transfer)方法如下, 当时存款(deposit)和取款(withdraw)方法都是抽象的, 形成一个AbstractAccount 类. 从AbstractAccount 类扩充出具体类, 实现继承来的抽象方法.
抽象类AbstractAccount

package model;

public abstract class AbstractAccount {
    public abstract int getBalance();

    public abstract void deposit(int amount);

    abstract public void withdraw(int amount);

    public void transfer(AbstractAccount from, AbstractAccount to, int amount) {
        from.withdraw(amount);
        to.deposit(amount);
    }
    public void transfer(AbstractAccount to, int amount) {
        this.withdraw(amount);
        to.deposit(amount);
    }
}

具体类Account

package model;

public class Account extends AbstractAccount {
    private String accountID;
    private String password;
    private int balance;
    public void deposit(int amount) {
        this.balance += amount;
    }
    public void withdraw(int amount){
        this.balance -= amount;
    }
    public Account(String accountID, String password, int balance) {
        this.accountID = accountID;
        this.password = password;
        this.balance = balance;
    }
    public Account(String accountID) {
        this(accountID, "123", 2000);
    }
    public Account() {
        this("6200", "123", 1000);
    }
    public String getAccountID() {
        return accountID;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
    public void setAccountID(String accountID) {
        this.accountID = accountID;
    }
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

接口与实现

应用场景

定义程序之间的接口规范(API, Application Programming Interface)时, 通常定义的方法的名称和传递的参数和返回值的数据结构. 在Java语言中, 用interface作为包含多个相关函数名称的结构.
如果一个抽象类中, 全部都是抽象方法, 那么可以把这个纯的抽象类改写为接口.

接口interface

用interface 而不是class 声明一个接口. 不需要使用abstract修饰方法. 接口中只能声明方法, 不可以在接口中实现方法.
接口之间可以继承.

实现接口implements

一个类可以实现多个接口, 用implements关键词而不是用extends. 如果一个类实现接口是, 没有为所有的方法提供实现, 也就是仍有未实现的方法, 那么这个类是抽象类. 如果实现了所有的方法, 那么是具体类.

应用示例

接口AccountInterface

package model;

public interface AccountInterface {
    public int getBalance();

    public void deposit(int amount);

    public void withdraw(int amount);
}

实现接口的类AccountInterfaceImpl

package model;

public class AccountInterfaceImpl implements AccountInterface {

    private int balance;

    @Override
    public void deposit(int amount) {
        this.balance += amount;
    }

    @Override
    public void withdraw(int amount) {
        this.balance -= amount;
    }

    @Override
    public int getBalance() {
        return this.balance;
    }
}

猜你喜欢

转载自blog.csdn.net/dlutcat/article/details/80203991