当我尝试编译时,它说 writeCheck 方法不存在并且我知道它与我正在调用该方法有关,有没有办法在使用 Account 类型时从 Checkings 类调用方法?我的哈希图有不同的账户类型,如果指定的账户是支票账户,我只想写一张支票。我已经实现了那个检查系统,但我仍然不知道如何访问子类的方法。
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Person
{
public final String name,address,phoneNumber;
public Random aNumGen;
public HashMap<Integer,Account> accounts;
public Integer accountNum;
public Person(String name,String address,String phoneNumber)
{
aNumGen=new Random();
accounts = new HashMap<Integer,Account>();
this.name=name;
this.address=address;
this.phoneNumber=phoneNumber;
}
public void addAccount(String accountType,double initialAmount,Integer numberOfYears)
{
do
{
accountNum = aNumGen.nextInt(999999);
}
while(accounts.containsKey(accountNum));
if(accountType.toLowerCase().contains("check"))
{
accounts.put(accountNum,new Checkings(name,address,phoneNumber,accountNum));
deposit(accountNum,initialAmount);
}
else if(accountType.toLowerCase().contains("sav"))
{
accounts.put(accountNum,new Savings(name,address,phoneNumber,accountNum));
deposit(accountNum,initialAmount);
}
else if(accountType.toLowerCase().contains("loan"))
{
accounts.put(accountNum,new HomeLoan(name,address,phoneNumber,accountNum,initialAmount,numberOfYears));
}
else
{
System.out.println("That account type does not exist.");
}
printAccounts();
}
public void printAccounts()
{
System.out.println(name +" " + address + " " + phoneNumber);
for(Map.Entry<Integer,Account> account: accounts.entrySet())
{
System.out.println(" " + account.getValue().getType()+ ": " + account.getKey() + " " + "$" + account.getValue().getBalance());
}
System.out.println();
}
这是我遇到麻烦的地方。这仍然是 person 类的一部分。
public void writeCheck(Integer accountNumber, String toPerson, Integer amount)
{
if(accounts.containsKey(accountNumber) && accounts.get(accountNumber).getType().equalsIgnoreCase("Checkings"))
{
accounts.get(accountNumber).writeCheck(toPerson, amount);
}
}
}
帐户超类。
public class Account
{
public double balance;
public final int accountNumber;
public String name, address, phoneNumber,type;
public Account(String name, String address, String phoneNumber, int accountNumber)
{
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.accountNumber = accountNumber;
}
public int getAccountNumber()
{
return accountNumber;
}
public void deposit(double amount)
{
balance += amount;
}
public void withdrawl(double amount)
{
if(amount <= balance)
{
balance-=amount;
}
}
public String getType()
{
return type;
}
public void closeAccount()
{
balance=0;
System.out.println("Your account has been closed.");
}
} 子类检查
import java.util.HashMap;
import java.util.Map;
public class Checkings extends Account
{
public HashMap<String,Integer> checkHistory;
public Checkings(String name, String address, String phoneNumber, int accountNumber)
{
super(name, address, phoneNumber, accountNumber);
checkHistory = new HashMap<String,Integer>();
type = "Checkings";
}
public void writeCheck(String toAccount, Integer amount)
{
withdrawl(amount);
checkHistory.put(toAccount, amount);
}
public void viewCheckHistory()
{
System.out.println("Account: " + getAccountNumber());
for(Map.Entry<String,Integer> check: checkHistory.entrySet())
{
System.out.println("To: " + check.getKey() + " Amount: " + check.getValue());
}
}
}