接口和实现类的多态性

一个简单的例子来说明不同实现类实现同一个接口的情况。假设我们有一个接口 PaymentInterface,它定义了支付系统中的支付操作。这个接口可以被不同的支付方式实现,比如信用卡支付、PayPal支付或者微信支付(具体实现类)。

首先,我们定义一个支付接口 PaymentInterface

public interface PaymentInterface {
    void pay(double amount);
    String getPaymentMethodName();
}

接下来,我们创建几个不同的实现类,每个类都实现了 PaymentInterface 接口,但提供了不同的支付方式:

  1. 信用卡支付实现
public class CreditCardPayment implements PaymentInterface {
    private String cardNumber;
    private String cardHolderName;

	//构造函数
    public CreditCardPayment(String cardNumber, String cardHolderName) {
        this.cardNumber = cardNumber;
        this.cardHolderName = cardHolderName;
    }

    @Override
    public void pay(double amount) {
        // 实现信用卡支付逻辑
        System.out.println("Paying " + amount + " using credit card " + cardNumber);
    }

    @Override
    public String getPaymentMethodName() {
        return "Credit Card";
    }
}
  1. PayPal支付实现
public class PayPalPayment implements PaymentInterface {
    private String email;

    public PayPalPayment(String email) { //构造函数
        this.email = email;
    }

    @Override
    public void pay(double amount) {
        // 实现PayPal支付逻辑
        System.out.println("Paying " + amount + " using PayPal to " + email);
    }

    @Override
    public String getPaymentMethodName() {
        return "PayPal";
    }
}
  1. 微信支付实现
public class WeChatPayment implements PaymentInterface {
    private String weChatId;

    public WeChatPayment(String weChatId) { //构造函数
        this.weChatId = weChatId;
    }

    @Override
    public void pay(double amount) {
        // 实现微信支付逻辑
        System.out.println("Paying " + amount + " using WeChat to " + weChatId);
    }

    @Override
    public String getPaymentMethodName() {
        return "WeChat Pay";
    }
}

现在,我们有一个支付系统,它可以通过接口 PaymentInterface 来处理支付,而不需要关心具体的支付方式。这允许支付系统以统一的方式调用不同的支付实现:

public class PaymentSystem {
    public void makePayment(PaymentInterface paymentMethod, double amount) {
        paymentMethod.pay(amount); // 调用支付方法,具体调用哪个,取决于接口对象的类型
        System.out.println("Payment method used: " + paymentMethod.getPaymentMethodName()); //打印支付方式
    }
}

使用 PaymentSystem 的示例代码:

public class Main {
    public static void main(String[] args) {
        PaymentSystem paymentSystem = new PaymentSystem();
		
		// 在申明PaymentInterface 对象时,根据对象参数动态地选择了不同实现类
        PaymentInterface creditCardPayment = new CreditCardPayment("1234567890", "John Doe"); 
        PaymentInterface paypalPayment = new PayPalPayment("john.doe@example.com");
        PaymentInterface weChatPayment = new WeChatPayment("johndoe_weixin");

        paymentSystem.makePayment(creditCardPayment, 100.00);
        paymentSystem.makePayment(paypalPayment, 50.00);
        paymentSystem.makePayment(weChatPayment, 200.00);
    }
}

输出结果:

Paying 100.0 using credit card 1234567890
Payment method used: Credit Card
Paying 50.0 using PayPal to john.doe@example.com
Payment method used: PayPal
Paying 200.0 using WeChat to johndoe_weixin
Payment method used: WeChat Pay

这个例子展示了如何通过接口来实现多态性,使得 PaymentSystem 可以无缝地与不同的支付实现类进行交互,而不需要了解它们具体的实现细节。这种设计提高了代码的灵活性和可扩展性。
当不同的实现类实现同一个接口时,它们实际上是在进行方法的重写(override)。在面向对象编程中,重写是指子类提供一个与父类或接口中具有相同名称和参数的方法的实现,从而实现对父类方法的扩展或修改。

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值