Java中主类中如何调用其他类的方法呢?如下程序:

public class CeShi{
public static void main(String args[]){
int array[]={1,5,6,7};
fun(array);
for (int x=0;x<array.length ;x++ ){
System.out.print("array["+ x +"]="+array[x]+"\n");
}
}
public static void fun(int temp[]){
temp[0]=140;
}
}
如果上边的打印方法是存在于其它的类之中 如何调用 刚才试了好多次都无法正常编译

第1个回答  2012-11-15
假如你有另外一个类 Test, 那么你要在CeShi里新建一个test类实例,然后call这个方法

Test t = new Test();
t.fun(xx); // 此时这个方法不需要static

你可以用匿名类 直接在Ceshi类里输入
new Test().fun(xx);

public class CeShi{
public static void main(String args[]){

int array[]={1,5,6,7};
Test t = new Test();
t.fun(array);

// new Test().fun(array); // 匿名类
for (int x=0;x<array.length ;x++ ){
System.out.print("array["+ x +"]="+array[x]+"\n");
}
}

}

class Test{
public void fun(int temp[]){
temp[0]=140;
}

}本回答被提问者采纳
第2个回答  2012-11-15
public class Print{
public static void fun(int temp[]){
temp[0]=140;
}
}
静态方法,在调用的时候只需使用类名.方法名即可,即Print.fun();本回答被网友采纳
第3个回答  2012-11-15
用类的对象进行调用,或者直接用其他类的类名进行调用,
要不就是导包import 包名
第4个回答  2012-11-15
public class A{
public static void print(int[] temp){

for (int x=0;x<temp.length ;x++ ){
System.out.print("array["+ x +"]="+temp[x]+"\n");
}

}

}

public class CeShi{
public static void main(String[] args){
int array[]={1,5,6,7};
fun(array);
A.print(array);

}

public static void fun(int temp[]){
temp[0]=140;
}

}
是这意思么