我有两个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;
}
}
答案 0 :(得分:1)
您需要的是将ArrayMath
中的方法定义为public
和static
,例如:
public static void arrayAddition(int a, int b)
{
// do something
}
答案 1 :(得分:0)
ArrayMath.java
的方法定义为static
。ArrayMath
,使其成为public
方法,然后从Assign_2
执行import ArrayMath
。 ArrayMath.yourMethod()
。答案 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);
答案 4 :(得分:0)
有多种方式
1.继承is-a
关系
2.合成has-a
关系 - 但如果has-a
关系,您必须向您的方法发出public
和static
来从另一个类调用。