在其他 class 中重载静态方法不是 运行?

Overloaded static methods not running in other class?

对于我正在做的练习,我有三种方法,所有 isEven 测试一个值,returns 真或假。一个 isEven 是一个实例方法,另外两个是静态方法,每个方法都有一个参数。如果我创建一个新的 testClass,并且 运行 main 方法中的方法实例 isEven 方法工作正常,但是当我尝试使用静态 isEven 方法时,我得到一个符号未找到错误。但是,如果我在实际应用程序 class 和 运行 中创建主要方法,那么重载方法将起作用。为什么会这样?静态方法不应该能够在另一个 class 中 运行 吗?使用的方法如下。

public class MyInteger {
    private int value;
    public boolean isEven() {
        if(this.value % 2 == 0) {
            System.out.println(value + " is an even number.");
            return true;
        }
        else {
            System.out.println(value + " is not an even number.");
            return false;
        }
    }

    public static boolean isEven(int value) {
        if(value % 2 == 0) {
            System.out.println(value + " is an even number.");
            return true;
        }
        else {
            System.out.println(value + " is not an even number.");
            return false;
        }
    }

    public static boolean isEven(MyInteger value) {
        if(value.value % 2 == 0) {
            System.out.println(value.value + " is an even number.");
            return true;
        }
        else {
            System.out.println(value.value + " is not an even number.");
            return false;
        }
    }
}
class testMyInteger {
    public static void main(String[] args) {
        MyInteger val1 = new MyInteger(2);
        int val = 2;
        System.out.println("The value of this object is " + val1.getValu());
        val1.isEven();
        val1.isOdd();
        val1.isPrime();
        isEven(val1);
    }
}

你有几个问题。它们在评论中有解释,但我会在这里列出它们:

  • 我在 post 中没有看到构造函数(在撰写本文时)
  • 您的 main() 方法在同一个 java 文件中,但不在同一个 class 中。它们是两种截然不同的东西。

    public class MyInteger {
        // Not included in post
        private int value;
        // Not included in post
        public MyInteger(int i) {
            value = i;
        }
    
        public boolean isEven() {
            if (this.value % 2 == 0) {
                System.out.println(value + " is an even number.");
                return true;
            } else {
                System.out.println(value + " is not an even number.");
                return false;
            }
        }
    
        public static boolean isEven(int value) {
            if (value % 2 == 0) {
                System.out.println(value + " is an even number.");
                return true;
            } else {
                System.out.println(value + " is not an even number.");
                return false;
            }
        }
    
        public static boolean isEven(MyInteger value) {
            if (value.value % 2 == 0) {
                System.out.println(value.value + " is an even number.");
                return true;
            } else {
                System.out.println(value.value + " is not an even number.");
                return false;
            }
        }
    
    }
    
    class testMyInteger {
        public static void main(String[] args) {
            MyInteger val1 = new MyInteger(2);
            int val = 2;
            //System.out.println("The value of this object is " + val1.getValue()); // mispelled here, Not declared anyways
            val1.isEven();
            // these are not declared
            //val1.isOdd();
            //val1.isPrime();
    
            // Not inside of the 'MyInteger class, therefor not able to be called without static reference of MyInteger.isEvent
            //isEven(val1);
            MyInteger(val1);
        }
    }
    

让我们看看您的代码:

public static void main(String[] args) {
    MyInteger val1 = new MyInteger(2);
    int val = 2;
    System.out.println("The value of this object is " + val1.getValu());
    val1.isEven();
    val1.isOdd();
    val1.isPrime();
    isEven(val1); // <-- here is the problem.
}

static 方法不像免费方法。您仍然需要使用 class 或通过 class.

的实例来调用它们

例如:

 public static void main(String[] args) {
    MyInteger val1 = new MyInteger(2);
    int val = 2;

    MyInteger.isEven(val1);
    val1.isEven(val1);

    MyInteger.isEven(val);
    val1.isEven(val);
}