如何将方法从A类传递到B类

发布于 2024-09-13 23:42:06 字数 1553 浏览 1 评论 0原文

有 3 个文件:

1. Annuuity.java
2. AnnuityDueGUI.java  // GUI for Annuity Due
2. AnnuityDueResultGUI.java  //GUI for the result

在 AnnuityDueGUI.java 下:

public double calculateFADGUI(){
        //FVA = A{[(1+i)^n – 1] / i} (1+i)
        String amountStr = amount.getText() ;  //convert string to double
        dAmount = Double.parseDouble(amountStr) ;
        String iStr = iText.getText() ;
        dInterest = Double.parseDouble(iStr) ;
        String periodStr = period.getText() ;
        dPeriod = Double.parseDouble(periodStr) ;
        iPeriod = (int)dPeriod ;
        due = new Annuity(dAmount, dInterest, iPeriod) ;
        System.out.println(due.calculateFAD()) ;
        return due.calculateFAD() ;  //calculateFAD() is under Annuity.java
        }

在 AnnuityDueResultGUI.java 下:

AnnuityDueGUI due ;

public AnnuityDueResultGUI(AnnuityDueGUI due){  //1st solution failed
    this.due = due ;
}
public void grabResult(){  //1st solution failed
   result = this.due.calculateFADGUI() ;
}

public AnnuityDueResultGUI(){

    JPanel p6 = new JPanel() ;
    p6.setLayout(new GridLayout(2, 1)) ;
    p6.add(new JLabel("you will have approximately $" + result)) ;
    // other codes 
}

从 AnnuityDueGUI.java 我可以看到 due.calculateFAD() 的结果。但是,我想在 AnnuityDueResultGUI.java 下显示结果,

我已经将它们放在名为“GUI”的包下,并且还导入了 AnnuityDueGUI.java 和 Annuity.java。

我使用未注册用户执行了本论坛中针对我的另一个相同问题所推荐的步骤。但是,它不起作用(通过的结果是0)。这就是为什么我再次发布相同的问题并提供更多详细信息。

请协助并提前致谢。

There are 3 files:

1. Annuuity.java
2. AnnuityDueGUI.java  // GUI for Annuity Due
2. AnnuityDueResultGUI.java  //GUI for the result

Under AnnuityDueGUI.java:

public double calculateFADGUI(){
        //FVA = A{[(1+i)^n – 1] / i} (1+i)
        String amountStr = amount.getText() ;  //convert string to double
        dAmount = Double.parseDouble(amountStr) ;
        String iStr = iText.getText() ;
        dInterest = Double.parseDouble(iStr) ;
        String periodStr = period.getText() ;
        dPeriod = Double.parseDouble(periodStr) ;
        iPeriod = (int)dPeriod ;
        due = new Annuity(dAmount, dInterest, iPeriod) ;
        System.out.println(due.calculateFAD()) ;
        return due.calculateFAD() ;  //calculateFAD() is under Annuity.java
        }

Under AnnuityDueResultGUI.java:

AnnuityDueGUI due ;

public AnnuityDueResultGUI(AnnuityDueGUI due){  //1st solution failed
    this.due = due ;
}
public void grabResult(){  //1st solution failed
   result = this.due.calculateFADGUI() ;
}

public AnnuityDueResultGUI(){

    JPanel p6 = new JPanel() ;
    p6.setLayout(new GridLayout(2, 1)) ;
    p6.add(new JLabel("you will have approximately $" + result)) ;
    // other codes 
}

From AnnuityDueGUI.java I am able to see the result of due.calculateFAD() . But, I would like to display the result under AnnuityDueResultGUI.java

I already did put them under a package named 'GUI' and also did import AnnuityDueGUI.java and Annuity.java.

I did the steps that was recommended in this forum on my other same question using unregisterd user. But, it did not work(the passed result is 0). That's why I'm posting the same question again with more details.

Please assist and thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2024-09-20 23:42:06

我认为您忘记调用 grabResult() 方法。

public AnnuityDueResultGUI(AnnuityDueGUI due){  
    this.due = due ;
    grabResult(); // calling this will store the value in the "result" instance variable

    JPanel p6 = new JPanel() ;
    p6.setLayout(new GridLayout(2, 1)) ;
    p6.add(new JLabel("you will have approximately $" + result)) ;
    // other codes 
}

private void grabResult(){  
   result = this.due.calculateFADGUI() ;
}

另外,您应该避免在构造函数中调用非最终类的公共方法。

I think you forgot to call the grabResult() method.

public AnnuityDueResultGUI(AnnuityDueGUI due){  
    this.due = due ;
    grabResult(); // calling this will store the value in the "result" instance variable

    JPanel p6 = new JPanel() ;
    p6.setLayout(new GridLayout(2, 1)) ;
    p6.add(new JLabel("you will have approximately $" + result)) ;
    // other codes 
}

private void grabResult(){  
   result = this.due.calculateFADGUI() ;
}

On a side note, you should avoid calling public methods of a non-final class in your constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文