Java 如何实现抽象类和继承类之间的交互?
我必须使用Java 如何实现抽象类和继承类之间的交互?,java,methods,overriding,abstract,Java,Methods,Overriding,Abstract,我必须使用@Override方法实现抽象类和继承类之间的交互。问题是我不知道怎么做,我的任务是: 我用3种方法创建了一个抽象类帐户(pay(int-amount),transfer(Account-Account,int-amount),addMoney(int-amount))。付款必须看起来像普通的扣减。我重写了这些方法。每个人都必须有一个平衡。我无法从SavingsAccount付款,只能转账和添加。它也不能降为负值CreditAccount不能有正余额-如果我从中支付,它会降为负,要返回
@Override
方法实现抽象类和继承类之间的交互。问题是我不知道怎么做,我的任务是:
我用3种方法创建了一个抽象类帐户(pay(int-amount)
,transfer(Account-Account,int-amount)
,addMoney(int-amount)
)。付款必须看起来像普通的扣减。我重写了这些方法。每个人都必须有一个平衡。我无法从SavingsAccount
付款,只能转账和添加。它也不能降为负值CreditAccount
不能有正余额-如果我从中支付,它会降为负,要返回0,我需要添加它CheckingAccount
实现了所有这些方法,但是不能下到负数
我必须创建3个帐户类型变量,并为它们提供3种不同的帐户类型。下面是代码:
public abstract class Account {
protected int amount;
protected int balance;
public Account(int amount, int balance) {
this.amount = amount;
this.balance = balance;
}
void pay(int amount) {
}
void transfer(Account account, int amount) {
}
void addMoney(int amount) {
}
}
public class CheckingAccount extends Account {
public CheckingAccount(int amount, int balance) {
super(amount, balance);
}
@Override
void pay(int amount) {
super.pay(amount);
}
@Override
void transfer(Account account, int amount) {
super.transfer(account, amount);
}
@Override
void addMoney(int amount) {
super.addMoney(amount);
}
}
public class CreditAccount extends Account {
public CreditAccount(int amount, int balance) {
super(amount, balance);
}
@Override
void pay(int amount) {
super.pay(amount);
}
@Override
void transfer(Account account, int amount) {
super.transfer(account, amount);
}
@Override
void addMoney(int amount) {
super.addMoney(amount);
}
}
public class SavingsAccount extends Account {
public SavingsAccount(int amount, int balance) {
super(amount, balance);
}
@Override
void transfer(Account account, int amount) {
balance -= amount;
}
@Override
void addMoney(int amount) {
super.addMoney(amount);
}
}
基类似乎必须实现一些公共功能,并且它没有任何抽象方法。因此,它被标记为抽象,以避免实例化 此外,似乎没有在帐户级别定义验证(金额是否应为正值) 然后,
CheckingAccount
的实现需要覆盖方法以验证结果余额:
public class CheckingAccount extends Account {
public CheckingAccount(int amount, int balance) {
super(amount, balance);
}
@Override
void pay(int amount) {
if (this.balance >= amount) {
super.pay(amount);
} else {
throw new IllegalArgumentException("Insufficient funds");
}
}
@Override
void transfer(Account recipient, int amount) {
if (this.balance - amount >= 0) {
super.transfer(recipient, amount);
} else {
throw new IllegalArgumentException("Insufficient funds for transfer from CheckingAccount");
}
}
@Override
void addMoney(int amount) {
if (this.balance + amount >= 0) {
super.addMoney(amount);
} else {
throw new IllegalArgumentException("Balance may not be negative for CheckingAccount");
}
}
}
对于SavingsAccount
pay
方法,必须重写以引发异常。
可能值得覆盖转账
和addMoney
以验证余额,类似于检查账户
public class SavingsAccount extends Account {
public SavingsAccount(int amount, int balance) {
super(amount, balance);
}
@Override
void pay(int amount) {
throw new RuntimeException("This method is not supported for Savings account");
}
}
最后一个是CreditAccount
,在使用pay
的默认实现时,您可能需要检查余额是否为正(尽管信用账户也应该有信用限额):
公共类CreditAccount扩展帐户{
公共信贷账户(整数金额、整数余额){
超级(金额、余额);
}
@凌驾
无效转账(账户、整笔金额){
如果(this.balance-amount我仍然不理解这个问题,这是一个糟糕的类设计(“”和违反)。由于您的特定帐户类型没有实现由帐户
强加的契约,因此它们不应扩展该基类。有几种方法可以解决此问题-可能最简单的方法是使用单独的多个“特征”接口,让每个类实现它所支持的特性。同意康拉德的观点,最好是分成三个接口。可转移的,可支付的,可添加的。@KonradRudolph和AbhirojPanwar你们只是想修复他的结构吗?或者你们理解这个问题了吗?如果你们理解,请帮助我们理解,我可以看到类结构re可能缺少,但问题从未解决,或者是吗?会员使用的金额是多少?
public class SavingsAccount extends Account {
public SavingsAccount(int amount, int balance) {
super(amount, balance);
}
@Override
void pay(int amount) {
throw new RuntimeException("This method is not supported for Savings account");
}
}
public class CreditAccount extends Account {
public CreditAccount(int amount, int balance) {
super(amount, balance);
}
@Override
void transfer(Account account, int amount) {
if (this.balance - amount <= 0) {
super.transfer(account, amount);
} else {
throw new IllegalArgumentException("Balance may not be positive for CreditAccount");
}
}
@Override
void addMoney(int amount) {
if (this.balance + amount <= 0) {
super.addMoney(amount);
} else {
throw new IllegalArgumentException("Balance may not be positive for CreditAccount");
}
}
}