Как вызвать методы дочернего класса из экземпляра родительского класса?JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Как вызвать методы дочернего класса из экземпляра родительского класса?

Сообщение Anonymous »

Код: Выделить всё

public class Account {
double currentBalance;

Account(double currentBalance) {
this.currentBalance = currentBalance;
}

double getBalance() {
return(this.currentBalance);
}

void deposit(double amount) {
this.currentBalance += amount;
}

void withdraw(double amount) {
this.currentBalance -= amount;
}

public String toString() {
return("Your current account balance is: $" + this.currentBalance);
}
}

Код: Выделить всё

 public class SavingsAccount extends Account {
double interestRate;

SavingsAccount(double interestRate, double amount) {
super(amount);
this.interestRate = interestRate;
}

void addInterest(double interestRate) {
this.currentBalance = this.currentBalance + (this.currentBalance * (interestRate / 100));
}

public String toString() {
return("Your current account balance is: $" + this.currentBalance + ". Your interest rate is: " + this.interestRate + "%");
}
}

Код: Выделить всё

public class AccountTester {
public static void main(String[] args) {
System.out.println("Welcome to the COMP 251 BANK");

Account[] acc = new Account[10];

acc[0] = new Account (100000); // initial amount of 100k
System.out.println(acc[0].toString());

acc[0].deposit(50000); //deposit 50k
System.out.println("Current balance after depositing is: " + "$" + acc[0].getBalance());

acc[0].withdraw(10000);
System.out.println("Current balance after withdrawing is: " + "$" + acc[0].getBalance());

System.out.println();
System.out.println("CHECKING ACCOUNT");

acc[1] = new CheckingAccount(10000,50000); //overdraft limit of 1000, initial amount of 50k
System.out.println(acc[1].toString());

acc[1].deposit(20000); //I dont know what to do about the chequeDeposit
System.out.println("Current balance after depositing is: " + "$" + acc[1].getBalance());

System.out.println();
System.out.println("SAVINGS ACCOUNT");

acc[2] = new SavingsAccount(10, 50000);
System.out.println(acc[2].toString());

acc[2].deposit(10000);
System.out.println("Current balance after depositing is: " + "$" + acc[2].getBalance());

acc[2].addInterest();
}
}
Я все еще новичок в этом, поэтому прошу прощения, если это плохой вопрос, но я попробовал поиграться с каждым классом и до сих пор не могу понять, как использовать Метод addInterest(), который я создал в классе SavingsAccount, который расширяет класс Account. Я попробовал SavingsAccount.addInterest(), но получил статическую ошибку ссылки. Я думал, что acc[2].addInterest() будет работать, но там написано, что он не определен для типа Account.

Подробнее здесь: https://stackoverflow.com/questions/721 ... s-instance
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»