gpt4 book ai didi

java - 返回一个字符串供父类(super class)的抽象方法显示

转载 作者:行者123 更新时间:2023-12-01 14:30:06 26 4
gpt4 key购买 nike

必须实现抽象方法statement(在父类(super class)中)才能返回语句的字符串表示形式。

所以我做了以下事情:

 public abstract String statement(); //The abstract method in my super class

..以及我的子类中的方法:

//@Override
public String statement()
{
return String.format("Account no %d has balance R%d and minimum balance R%d", accountNumber,balance,getMinBalance());
}

我的主类只是调用 Account 类(相关的父类(super class)),如下所示:

new SavingsAccount(Integer.toString(ao[i]),ao[i+1],ao[a]); //ao being the array which contains the values.

但是,控制台只是终止而不显示任何内容(我也不熟悉实现)。

完整代码如下:

主要:

public class AccountList 
{
public static void main(String[] args)
{
int[] ao = {00000,0,0,12345,500,250,23456,230,-50,34567,340,500,45678,-320,-50,56789,-320,-500};

for(int i=0;i<ao.length;i=i+3)
{
int a = i+2;
if(ao[a]>=0)
{

new SavingsAccount(Integer.toString(ao[i]),ao[i+1],ao[a]);
}
if(ao[a]<=0)
{

new ChequeAccount(Integer.toString(ao[i]),ao[i+1],ao[a]);
}
}
}
}

super 类:

public abstract class Account implements InterestAccount
{
static String accountNumber;
int balance;

public Account()
{
accountNumber = "00000";
balance = 0;
//statement();
}
public Account(String accountNumber,int balance)
{
setAccountNum(accountNumber);
setBalance(balance);
}
public void setAccountNum(String accNum)
{
accountNumber = accNum;
}
public void setBalance(int balance)
{
this.balance = balance;
}
public String getAccountNumber()
{
return accountNumber;
}
public int getBalance()
{
return balance;
}
public abstract String statement();
}

子类之一:

public class SavingsAccount extends Account
{
int minBalance;

public SavingsAccount()
{
super();
minBalance = 0;
}
public SavingsAccount(String accountNum,int minBalance,int balance)
{
super(accountNum,balance);
setMinBalance(minBalance);
}
public void setMinBalance(int minBalance)
{
this.minBalance = minBalance;
}
public int getMinBalance()
{
return minBalance;
}
@Override
public int calculateInterest(int value) {

if(minBalance>balance)
{
return 0;
}
else
{
return (minBalance*balance)/100;
}
}
//@Override
public String statement()
{
return String.format("Account no %d has balance R%d and minimum balance R%d", accountNumber,balance,getMinBalance());
}

}

最佳答案

你永远不会调用方法语句。

1) 创建一个新的 SavingsAccount 对象

new SavingsAccount(Integer.toString(ao[i]),ao[i+1],ao[a]);

2)构造函数类被上述语句调用

public SavingsAccount(String accountNum,int minBalance,int balance)
{
super(accountNum,balance);
setMinBalance(minBalance);
}

3) 子类然后调用父类(super class)方法 公共(public)账户(String accountNumber,int 余额) { setAccountNum(帐号); 设置余额(余额); }

4)调用SetBalance

public void setBalance(int balance)
{
this.balance = balance;
}

5) setMinBalance 调用

public void setMinBalance(int minBalance)
{
this.minBalance = minBalance;
}

6) 创建对象SavingsAccount结束

没有单个语句调用方法语句

关于java - 返回一个字符串供父类(super class)的抽象方法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16919763/

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