大家好,我有一个问题。当我尝试将对象数组传递给方法时出现错误 我的类(class)
public class Object {
private int x1;
public Object(int a ,){
this.x1=a;
}
public class staticMethods{
public static void findMaxPos(Object[] name){
max = name[0]
pos = 0
for( int i=1; i<name.length ; i++){
if ( name[i] >max ){
max = name[i];
pos = i;
}
}
}
public class Example{
public static void main(String[] args) {
Object[] yp = new Object2[3];
yp[0] = new Object(5);
yp[1] = new Object(6);
yp[2] = new Object(8);
findMaxPos(type)// i get an error of the method findMaxPos is undefined for the type Example
}
很抱歉这篇文章很长......
最佳答案
findMaxPos 是类 staticMethod 的静态方法。
当你不在定义它的类中调用静态函数时,你需要使用之前的类名来调用它:
public static void main(String[] args) {
Object[] type = new Object2[3];
yp[0] = new Object(5);
yp[1] = new Object(6);
yp[2] = new Object(8);
staticMethods.findMaxPos(type);// This should be ok.
}
请注意,在 java 中,约定是为类指定一个以大写字母开头的名称(以小写字母开头的名称用于实例)。
关于java - 将对象数组传递给java中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534062/