how to pass a method from different class to anoth

2019-08-05 21:55发布

There are 2 files named:

  1. AnnuityDueGUI.java
  2. AnnuityDueResultGUI.java

Under AnnuityDueGUI.java, there is this method as below:

=============

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, how to grab the result from the method that I've stated above?both classes are under the same package "GUI". I also did import AnnuityDueGUI.* ;

But still have no idea on how to grab the result from AnnuityDueGUI.java and display it under AnnuityDueResultGUI.java.

Please assist and thanks in advance.

3条回答
狗以群分
2楼-- · 2019-08-05 22:10
class AnnuityDueGUI {

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
    }

//...other code
}

class AnnuityDueResultGUI {

AnnuityDueGUI var = new AnnuityDueGUI();
double result = var.calculateFADGUI();
//other code
}
查看更多
倾城 Initia
3楼-- · 2019-08-05 22:25

Create AnnuityDueResultGUI with a reference to the actual AnnuityDueGUI instance, then you can access the method:

public class AnnuityDueResultGUI {
  private AnnuityDueGUI parent;

  public AnnuityDueResultGUI(AnnuityDueGUI parent) {
    this.parent = parent;
  }

  public void somemethod() {
    parent.calculateFADGUI();  // now you can access a method from a AnnuityDueGUI  instance
  }
}

and somewhere inside a method of AnnuityDueGUI:

// ...
AnnuityDueResultGUI resultGUI = new AnnuityDueResultGUI(this);
// ...
查看更多
霸刀☆藐视天下
4楼-- · 2019-08-05 22:30

You will need a reference to the AnnuityDueGUI object in AnnuityDueResultGUI. So for instance

AnnuityDueGUI adg = new AnnuityDueGUI()
double result = adg.calculateFADGUI()

UPDATE:

Also if you happen to already be constructing the AnnuityDueGUI somewhere else you can just pass the reference to AnnuityDueResultGUI

public AnnuityDueResultGui(AnnuityDueGUI adg) {
    this.adg = adg;
}

private void otherFunc () {
    double results = this.adg.calculateFADGUI()
}
查看更多
登录 后发表回答