packagecn.imcompany.pay;/**
* Created by tomyli on 2018/6/20.
* Github: https://github.com/peng051410
*/publicinterfacePayment{booleanpay(Stringparam);}
packagecn.imcompany.pay;/**
* Created by tomyli on 2018/6/20.
* Github: https://github.com/peng051410
*/publicclassAliPayimplementsPayment{@Overridepublicbooleanpay(Stringparam){System.out.println("AliPay");returntrue;}}publicclassWechatPayimplementsPayment{@Overridepublicbooleanpay(Stringparam){System.out.println("WechatPay");returntrue;}}
packagecn.imcompany.pay;/**
* Created by tomyli on 2018/6/20.
* Github: https://github.com/peng051410
*/publicenumPayType{ALI_PAY(newAliPay()),WECHAT_PAY(newWechatPay()),JD_PAY(newJdPay()),UNICOME_PAY(newUnicomePay()),;privatePaymentpayment;PayType(Paymentpayment){this.payment=payment;}publicPaymentgetPayment(){returnpayment;}}
在常量类可以获取要支付的具体支付方式来进行支付。接下来进行一下测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
packagecn.imcompany.pay;/**
* Created by tomyli on 2018/6/20.
* Github: https://github.com/peng051410
*/publicclassPayTest{publicstaticvoidmain(String[]args){PayType.ALI_PAY.getPayment().pay("123");PayType.valueOf(PayType.WECHAT_PAY.toString()).getPayment().pay("325");}}