简体   繁体   English

返回一个字符串以供Super类的抽象方法显示

[英]Returning a String for a Super class' abstract method to display

The abstract method statement (in the super class) must be implemented to return a string representation of a statement. 必须实现抽象方法语句 (在超类中)以返回语句的字符串表示形式。

So I've done the following: 所以我做了以下工作:

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

..and the method in my subclass: ..和我的子类中的方法:

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

My main class just calls the Account class (the super class in question) as follows: 我的主类仅按以下方式调用Account类(所讨论的超类):

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

However, the console just terminates without displaying anything (I'm also not familiar with implementation). 但是,控制台只是终止而不显示任何内容(我也不熟悉实现)。

Here's the full code: 这是完整的代码:

Main: 主要:

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 class: 超类:

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

One of the sub-classes: 子类之一:

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

}

You never calls the method statement. 您永远不会调用方法语句。

1) create a new SavingsAccount object 1)创建一个新的SavingsAccount对象

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

2) Constructor class got call by above statement 2)构造函数类被上述语句调用

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

3) Subclass then call superclass method public Account(String accountNumber,int balance) { setAccountNum(accountNumber); 3)子类然后调用超类方法public Account(String accountNumber,int balance){setAccountNum(accountNumber); setBalance(balance); setBalance(balance); } }

4) SetBalance is called 4)调用SetBalance

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

5) setMinBalance called 5)setMinBalance调用

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

6) End of create object SavingsAccount 6)创建对象SavingsAccount结束

No single statement calling the method statement 没有单个语句调用方法语句

It might help to know more of your code, but from what I see, there are some possible cases: 了解更多您的代码可能会有所帮助,但是据我所知,有一些可能的情况:

  • Your code runs and terminates without an error. 您的代码运行并终止,没有错误。 Reason you don't see anything is, there is nothing written to the standard output stream (eg via System.out.println()). 原因是您看不到任何东西,因此没有任何内容写入标准输出流(例如,通过System.out.println())。
  • Your code has some internal problems, which cause an exception, which again is not shown due to a broken logging mechanism. 您的代码有一些内部问题,这些问题会导致异常,由于日志记录机制损坏,该异常也不会显示。

In any case, use a debugger to step through you code and determine which parts run perfectly and if any fail. 在任何情况下,都可以使用调试器逐步执行代码,并确定哪些部分可以正常运行以及是否有任何故障。 It might help to extract some local variables, give them proper meaningful names and watch their contents during execution. 这可能有助于提取一些局部变量,为它们赋予适当的有意义的名称,并在执行期间监视其内容。

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

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