gpt4 book ai didi

java - 引用父类(super class)时如何调用子类的方法?

转载 作者:行者123 更新时间:2023-12-02 06:32:24 25 4
gpt4 key购买 nike

当我尝试编译时,它说 writeCheck 方法不存在,我知道它与我调用该方法有关,有没有办法在使用 Account 类型时从 Checkings 类调用方法?我的 HashMap 有不同的帐户类型,我只想在指定的帐户是支票帐户时能够写支票。我已经实现了该检查系统,但我仍然不知道如何访问子类的方法。

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);
}
}

}

帐户父类(super class)。

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());
}
}

}

最佳答案

如果您有 Account 类型的变量,则它没有 writeCheck 方法,即使实际实例是 Checkings。因此,您需要做的是将其转换为正确的子类 - 仅当它确实是正确的子类类型时才有效:

Account a = new Checkings(...); // this is ok
a.writeCheck(...); // you can't do this

// cast to subtype
Checkings checkingsAccount = (Checkings) a;

checkingsAccount.writeCheck(...); // this should work

更好的方法可能是为所有帐户类型使用具有相同签名但具有不同代码的方法,例如

(在帐户中)

abstract class Account {
abstract void makePayment(String accountNumber, int amount);
}

(在检查中)

class Checkings extends Account {

void makePayment(String accountNumber, int amount){
// this is a checkings account, so put the "writeCheck" code here
}
}

这样,您就不必进行转换,甚至不必关心您正在使用的帐户类型,只需执行以下操作:

Account a = new Checkings(...); // or any Account subclass
a.makePayment(accountNumber, amount);

不过,您可能应该检查错误 - 例如确保确实存在具有该号码的帐户,帐户中有足够的现金或信用等。但我认为这并不是真正的现实生活中的金融系统.

关于java - 引用父类(super class)时如何调用子类的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com