简体   繁体   English

我可以在超类的数组上调用子类吗?

[英]can I call subclasses on an array of a superclass?

If I have an array of an object that has subclasses, can I access methods in the subclasses through calling the array? 如果我有一个包含子类的对象的数组,是否可以通过调用该数组来访问子类中的方法?

This is a method in my Banking class, I would like to set the checking: 这是银行类中的一种方法,我想设置检查:

public void deposit() throws IOException {
BufferedReader br;
String entered_amount;

System.out.print("How much would you like to deposit? :");
br = new BufferedReader(new InputStreamReader(System.in));
entered_amount = br.readLine();
double amount = Double.valueOf(entered_amount).doubleValue();
balance = balance + amount;

System.out.println("Your balance is: " + getBalance());

Here is my Checking class: 这是我的Checking课:

public class Checking extends Bankaccount {

private double balance;

public Checking() {
}

public Checking(double balance) {
    this.balance = balance;
}

/**
 * @return the balance
 */
public double getBalance() {
    return balance;
}

/**
 * @param balance the balance to set
 */
public void setBalance(double balance) {
    this.balance = balance;
}

} }

When calling baArray[0] in my main class is there a way to make deposit set the value of balance within the checking class? 在我的主类中调用baArray[0] ,是否有一种方法可以使存款设置支票类中的余额值?

The reason I'm doing this is because I'm writing out the array of objects to a file and rereading them when the program starts. 我这样做的原因是因为我正在将对象数组写到文件中,并在程序启动时重新读取它们。 I need to be able to store 3 sets of checking and savings. 我需要能够存储3套支票和储蓄。 My deposit method would be what i'm trying to edit to use inheritance to set the balance in checking. 我的存款方法将是我尝试编辑以使用继承设置支票余额的方法。

Add to your Banking class: 添加到您的Banking课程:

public abstract setBalance(double balance);

If you have subclasses of Banking besides the Checking class for which a setBalance method doesn't make sense, then you can implement the method and simply have it throw an exception. 如果除了setBalance方法没有意义的Checking类之外,还有Banking subclasses ,则可以实现该方法,并使其简单地引发异常。 (It would seem to make sense for a Savings class to also have a setBalance method though.) (对于Savings类,虽然也有setBalance方法似乎setBalance 。)

Alternatively, you can just move Checking 's setBalance method up to the superclass Banking , then Checking and Savings will both inherit the setBalance method. 或者,您可以将CheckingsetBalance方法移至超类Banking ,然后CheckingSavings都将继承setBalance方法。 If you need to override them, you can, no problem. 如果您需要覆盖它们,可以没有问题。

EDIT: As per the comments to this answer, it's now more clear to me what you're trying to accomplish. 编辑:根据对此答案的评论,现在对我来说更清楚了您要完成的工作。

If you're intending to have an array of Banking objects, and each object has some unique account number, and for every Bank account, there is a Checking account and a Savings account, then logically, it sounds like it makes more sense for Checking and Savings to be private classes that can only be accessed through the Banking class. 如果您打算有一组Banking对象,并且每个对象都有一个唯一的帐号,并且对于每个Bank帐户,都有一个Checking帐户和一个Savings帐户,那么从逻辑上讲,听起来对于Checking来说更有意义和Savings是只能通过Banking类访问的private类。

So when an account holder goes to the bank, they open a Banking account. 因此,当帐户持有人去银行时,他们会开设一个Banking帐户。 Perhaps by default this comes with 1 Checking account and 1 Savings account. 也许默认情况下,它带有1个Checking帐户和1个Savings帐户。 These would be class variables in your Banking class: 这些将是您的Banking类中的类变量:

private Checking checking;
private Savings savings;

Now your Banking class needs public methods to access the checking and savings accounts. 现在,您的Banking类需要公共方法来访问支票帐户和储蓄帐户。

Let's say your Checking and Savings class both have a getBalance and setBalance . 假设您的Checking and Savings类都具有getBalancesetBalance Now your Banking class would need a setter & getter for each type of account, and it just calls the appropriate method. 现在,您的Banking类将为每种类型的帐户都需要一个settergetter ,它只是调用适当的方法。

For example, in your Banking class: 例如,在您的Banking课程中:

public void setCheckingBalance(double balance)
{
    checking.setBalance(balance);
}

public double getCheckingBalance()
{
    return checking.getBalance();
}

Then in your main method, you'll use these methods via: 然后在您的主要方法中,将通过以下方式使用这些方法:

baArr[0].setCheckingBalance(100.00);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM