继承的简单代码示例
发布: (2026年3月16日 GMT+8 06:09)
2 分钟阅读
原文: Dev.to
Source: Dev.to
银行系统的简单编码示例
package bank.task;
public class BankAccount {
int accountNumber;
double balance;
public void deposit(double depositAmount, int accNo, String AccType) {
double total = balance + depositAmount;
System.out.println("An amount of Rs." + depositAmount + " has been deposited succesfully ");
System.out.println("The total amount in your " + AccType + " Account of Account number" + accNo + " is " + total);
}
protected void withdraw(double withdrawAmount, int accNo, String AccType) {
double total = balance - withdrawAmount;
System.out.println("An amount of Rs." + withdrawAmount + " has been withdrawn succesfully ");
System.out.println("The total amount in your " + AccType + " Account of your Account number" + accNo + " is " + total);
}
}
class SavingAccount extends BankAccount {
double interestRate;
public void displayInterest(double rate) {
System.out.println("The interest rate is" + rate);
}
}
class CurrentAccount extends BankAccount {
int limit = 2000;
public void payPenalty(float balance) {
if (balance < limit)
System.out.println("Your balance is less than Rs. " + limit + " . Please pay a penalty of Rs.500");
else
System.out.println("Your balance is above than Rs. " + limit + " .NO penalty");
}
}
class User {
public static void main(String args[]) {
SavingAccount sa = new SavingAccount();
CurrentAccount ca = new CurrentAccount();
sa.balance = 10000.00;
sa.deposit(2000.00, 1234567, "Savings");
sa.withdraw(2345.00, 1234567, "Savings");
sa.displayInterest(6.7F);
ca.balance = 1900;
ca.payPenalty((float) ca.balance);
}
}