如何在我创建的包中使用类中的方法

时间:2022-08-15 22:58:00

i'm making a calculator program in java. I created a package calculator which has 4 classes. The package takes in an equation and finds its result (this program works). Now I want to use this logic for a graphic calculator so I wrote

我正在用java制作一个计算器程序。我创建了一个包含4个类的包计算器。该包接受一个等式并找到其结果(该程序有效)。现在我想将这个逻辑用于图形计算器,所以我写了

package calculator; 

at the top of each class (made sure it was the first line in each class). I then wrote

在每个班级的顶部(确保它是每个班级的第一行)。然后我写道

import calculator.JCalc; 

in my class with the graphic calculator the class is also called Calculator (JCalc is the class with all the logic for performing the calculations). In my class Calculator id like to perform the method findResult when the user hits the "=" button but I'm not sure how to access this method inside the class Calculator can someone please tell me how I can do this.

在我的类中使用图形计算器,该类也称为Calculator(JCalc是具有执行计算的所有逻辑的类)。在我的类计算器ID中,当用户点击“=”按钮时执行方法findResult但是我不知道如何在类中访问此方法计算器可以有人请告诉我如何做到这一点。

private String find_result(String equation) {

    ...

}

3 个解决方案

#1


1  

What @Marc provided should work providing the method you want to call within the Calculator Class is declared as public:

@Marc提供的应该是什么,提供你想在Calculator类中调用的方法被声明为public:

Calculator calc = new Calculator(); 
System.out.println(calc.find_result("3+3"));

#2


0  

You need to make a Calculator object you can call from...

您需要创建一个可以调用的Calculator对象...

Calculator calc = new Calculator(); calc.(some methods)

计算器calc = new Calculator();计算(某些方法)

#3


0  

Like this my friend :)

像这样我的朋友:)

package calculator; 
import calculator.JCalc; 
import java.util.Scanner;

public class SOMECLASSNAME()
{ 
   Calculator calc = new Calculator();
   Scanner scanner = new Scanner(System.in);

   public static void main(String[] args)
   {
       String myEquation = SOME EQUALTION
       printResult(myEquation);
   }

   public void printResult(String myEquation)
   {
      if (scanner.equals("=")) 
         System.out.println(calc.find_result(myEquation))
      else printResult(); // recalls the method until "=" is typed.
   }   
}

#1


1  

What @Marc provided should work providing the method you want to call within the Calculator Class is declared as public:

@Marc提供的应该是什么,提供你想在Calculator类中调用的方法被声明为public:

Calculator calc = new Calculator(); 
System.out.println(calc.find_result("3+3"));

#2


0  

You need to make a Calculator object you can call from...

您需要创建一个可以调用的Calculator对象...

Calculator calc = new Calculator(); calc.(some methods)

计算器calc = new Calculator();计算(某些方法)

#3


0  

Like this my friend :)

像这样我的朋友:)

package calculator; 
import calculator.JCalc; 
import java.util.Scanner;

public class SOMECLASSNAME()
{ 
   Calculator calc = new Calculator();
   Scanner scanner = new Scanner(System.in);

   public static void main(String[] args)
   {
       String myEquation = SOME EQUALTION
       printResult(myEquation);
   }

   public void printResult(String myEquation)
   {
      if (scanner.equals("=")) 
         System.out.println(calc.find_result(myEquation))
      else printResult(); // recalls the method until "=" is typed.
   }   
}