【问题标题】:Overloaded static methods not running in other class?重载的静态方法不在其他类中运行?
【发布时间】:2016-08-11 18:07:13
【问题描述】:

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

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);
    }
}

【问题讨论】:

  • 正如提示:在 java 中重载静态方法没有意义,因为这是不可能的......
  • 你能添加一个堆栈跟踪吗?
  • 您应该向我们展示您的代码在不工作时的外观(将静态方法放在与主类不同的类中)。因为现在,您只是向我们展示了确实有效的代码,因此更难调试。
  • @ΦXocę웃Пepeúpaツ 你的意思是覆盖吗?因为在 Java 中重载静态方法绝对是可能的。
  • 是的......覆盖......对不起

标签: java static instance overloading


【解决方案1】:

让我们看看你的代码:

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 方法不像免费方法。您仍然需要使用类或通过该类的实例来调用它们。

例如:

 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);
}

【讨论】:

  • 是的,这行得通。非常感谢。这也解释了为什么 isEven 静态方法只与 isEven 一起工作,即使主要方法在 MyInteger 类下。
【解决方案2】:

这里有几个问题。它们在 cmets 中进行了解释,但我将在此处列出:

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

    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);
        }
    }
    

【讨论】:

  • @GhostCat 那更好吗?
  • 我不认为我在这个练习中覆盖了任何东西。所有三个 isEven 方法都写在一个类(MyInteger)中,然后我只在一个 testApp 类中运行它们。
  • Opps 误解了标题。给我一秒钟。
  • 我确实拥有我实际程序中需要的所有构造函数。我只是没有在这里添加它们,因为我只是遇到了 isEven 静态方法的问题。如果我在不同的类中有 main 方法,静态方法不应该仍然有效吗?我的实例化 isEven 方法可以在另一个类的 main 方法中运行,但静态方法不会运行。
猜你喜欢
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多