如何将Java文件方法导入另一个Java文件?

时间:2015-01-19 08:08:18

标签: java methods

我有两个Java文件,一个名为Assign_2,另一个名为ArrayMath。我想允许在ArrayMath中使用Assign_2方法,如下所示:

ArrayMath.arrayAddition(a, b); //Called ArrayMath from Assign_2.java

哪些代码允许ArrayMath.java使用Assign_2.java方法?

public class Assign_2 {

   /**
   * Main method to execute whole code.
   *
   * @param theArgs is used to keep consistency
   */

   public static void main(String[] theArgs) {
       Scanner input = null; // For file input
       PrintStream output = null; // For file output
       String inFileName = "in2a.txt"; //Input file and outputs
       String outFileName = "out.txt"; 
       boolean filesOk = false; // Checks for files to be accessed
       int[][] a = null; // Declaring the 4 arrays for operations
       int[][] b = null;
       int[][] c = null;  
       int[][] d = null;

       // Opens file for input and readys for output
       try {
         input = new Scanner(new File(inFileName));
         output = new PrintStream(new File(outFileName));
         filesOk = true;
         }
       catch (FileNotFoundException e) {
         System.out.println("Can't open file - " + e);
         }
       if (filesOk) {
         a = get2DArray(input);
         b = get2DArray(input);
         c = get2DArray(input);
         d = get2DArray(input);
         // Sanity check for what is in the array and if right
         //System.out.println(Arrays.deepToString(a));
         //System.out.println(Arrays.deepToString(b));
         //System.out.println(Arrays.deepToString(c));
         //System.out.println(Arrays.deepToString(d));

         // Calling to ArrayMath.java for operation results
         // Declaring arrays for final results
         int[][] sum = ArrayMath.arrayAddition(a, b);
         System.out.println(Arrays.deepToString(sum));
         //int[][] difference = ArrayMath.arraySubtraction(a, b);
         //int[][] multiplication = ArrayMath.arrayMultiply(a, b);
         }        
   }

   /**
   * get2DArray reads input file then creates 2D array
   * from the file.
   * 
   * @param input is a Scanner to input the file
   *
   * @return a 2D integer array filled with input values
   */

   public static int[][] get2DArray(Scanner theIn) {
      int rowSize = theIn.nextInt(); // Takes the first 2 numbers
      int colSize = theIn.nextInt(); // that provided column + row #'s
      int[][] a = new int[rowSize][colSize]; // New array with ^ #'s

      // For loops that add integers to arrays
      for (int i = 0; i < a.length; i++) {
         for (int k = 0; k < a[i].length && theIn.hasNextInt(); k++) {
            a[i][k] = theIn.nextInt();
         }
      }
      return a; // Returns value to main method that was called from        
   }   
}

ArrayMath.java是:

 public class ArrayMath {

   /**
   * Addition for 2D Arrays method
   * 
   * @param theA and theB are 2D arrays from Assign_2
   *
   * @return sum to Assign_2 main program
   */

   public static int[][] arrayAddition(int[][] theA, int[][] theB) {
      int[][] sum = new int[theA.length][theA.length]; 
      for (int i = 0; i < theA.length; i++) {
         for (int k = 0; k < theA[i].length; k++) {
            sum[i][k] = theA[i][k] + theB[i][k];
         }        
      }  
      return sum;  
  }   
}

5 个答案:

答案 0 :(得分:1)

您需要的是将ArrayMath中的方法定义为publicstatic,例如:

public static void arrayAddition(int a, int b)
{
  // do something
}

答案 1 :(得分:0)

  1. 将您想要导入ArrayMath.java的方法定义为static
  2. 假设包装类刚刚调用ArrayMath,使其成为public方法,然后从Assign_2执行import ArrayMath
  3. 请参阅ArrayMath.yourMethod()
  4. 等方法

答案 2 :(得分:0)

在为ArrayMath class:

创建对象后调用该方法
ArrayMath ob = new ArrayMath();

然后调用Assign_2类中的方法

ob.arrayAddition(a, b);

确保方法为public

答案 3 :(得分:0)

您需要做的就是从ArrayMath创建一个对象并使用Assign_2

中声明的方法

<强> E.g:

ArrayMath obj = new ArrayMath();
obj.arrayAddition(a, b);

详细了解creating objects in Java

答案 4 :(得分:0)

有多种方式 1.继承is-a关系
2.合成has-a关系 - 但如果has-a关系,您必须向您的方法发出publicstatic来从另一个类调用。