[Java Design Pattern] - Abstract class
Sunday, October 22, 2017
abstract
—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.abstract void moveTo(double deltaX, double deltaY);
Note: If a classes included a abstract method, the class itself must be declared abstract, as in:
public abstract class GraphicObject { // declare fields // declare nonabstract methods abstract void draw(); }
package com.designpattern.learn.section3.chapter4.pratice1; import java.math.BigDecimal; public abstract class Bank { private BigDecimal amount; public Bank(BigDecimal initializeAmount) { this.amount = initializeAmount; } public void deposit(BigDecimal drawAmount) { this.amount = this.amount.subtract(drawAmount); } public void setDeposit(BigDecimal addDeposit) { this.amount = this.amount.add(addDeposit); } public BigDecimal getDeposit() { return this.amount; } public void balanceEnquires() { System.out.println("Your deposit: " + this.amount); } public abstract void checking(); public abstract void chargeServiceFee(BigDecimal giveInterest); public abstract void giveInterest(BigDecimal amount); }
SavingAccount
package com.designpattern.learn.section3.chapter4.pratice1; import java.math.BigDecimal; public class SavingAccount extends Bank { /** * Constructs a <code>SavingAccount</code>. * * @param initializeAmount */ public SavingAccount(BigDecimal initializeAmount) { super(initializeAmount); } /** {@inheritDoc} */ @Override public void checking() { System.out.println("Saving Account don't checking fee."); } /** {@inheritDoc} */ @Override public void chargeServiceFee(BigDecimal giveInterest) { System.out.println("Saving Account don't charge Service Fee."); } /** {@inheritDoc} */ @Override public void giveInterest(BigDecimal amount) { System.out.println("You give interest: " + amount); setDeposit(getDeposit().add(amount)); System.out.println("Your deposit change:" + getDeposit()); chargeServiceFee(amount); } }
CheckingAccount
package com.designpattern.learn.section3.chapter4.pratice1; import java.math.BigDecimal; public class CheckingAccount extends Bank { // assume 5% fee of depositAmount private static final BigDecimal FIVE_SERVICE_FEE = new BigDecimal(0.05); /** * Constructs a <code>CheckingAccount</code>. * * @param initializeAmount */ public CheckingAccount(BigDecimal initializeAmount) { super(initializeAmount); } /** {@inheritDoc} */ @Override public void checking() { balanceEnquires(); } /** {@inheritDoc} */ @Override public void chargeServiceFee(BigDecimal giveInterest) { // calculator amount of service fee. BigDecimal serviceFee = BigDecimal.ZERO; serviceFee = giveInterest.multiply(FIVE_SERVICE_FEE); // subtract fee into deposit. System.out.println("Service Fee: " + serviceFee); setDeposit(getDeposit().subtract(serviceFee)); } /** {@inheritDoc} */ @Override public void giveInterest(BigDecimal amount) { System.out.println("Your account don't use Give Interest."); } }
Bài liên quan
Comments[ 0 ]
Post a Comment