我想称这种方法为:
public int ArraySum(int[] a)
{
int sum = 0;
int Element;
for(Element = 0; Element < a.length; Element++)
{
sum = sum + a[Element];
}
return sum;
}
在这个方法中(在不同的类中):
public int Mean()
{
return (something.ArraySum(a))/2;
}
我知道我可能需要创建一个对象,但我不确定是如何创建的。
发布于 2016-03-15 00:10:29
举个例子:
public class C1
{
//all the fields and stuff
public int hello(int a)
{
//all the code
}
public static int hey(int a)
{
//all code
}
}
注意:其中一个函数是静态的。观察我们怎么称呼他们。
public class C2
{
//all fields and stuff
public void callerFunction()
{
C1 obj=new C1();
//created an object of class C1
obj.hello(5);
C1.hey(10);
//only class name is required while calling static methods.
}
}
发布于 2016-03-15 00:10:47
您需要创建类ArraySum
方法的对象。如果它存在于Calculator
类中,如下所示:
public class Calculator{
public int ArraySum(int[] a){
int sum = 0;
int Element;
for(Element = 0; Element < a.length; Element++)
{
sum = sum + a[Element];
}
return sum;
}
}
然后,您需要做的是(假设该类没有定义任何非零参数构造函数),
Calculator calculator = new Calculator();
calculator.ArraySum(..);
https://stackoverflow.com/questions/36000365
复制相似问题