我可以从静态方法调用非静态方法吗?

时间:2013-11-25 16:55:33

标签: java static

我已经在这里展示了全班,以防我需要特别改变。我想在main方法中调用“power”方法,正如您在代码中看到的那样,但它不起作用,因为其中一个方法是静态的而另一个方法不是。有谁知道解决这个问题的方法吗?所有帮助表示赞赏!

public class Power
{
    public  int square(int x){
        return x*x;
    }

    public int cube(int x){
        return power(x, 3);
    }

    public int hypercube(int x){
        return power(x, 4);
    }

    public  int power(int x, int n)
    {
        if (n==1)
           System.out.println(n);

        if (n==2)
            square(x);

        if (n==3)
            cube(x);

        if (n==4)
            hypercube(x);
    }

    public static void main(String[] args)
    {
        int x = 6;

        Power p = new Power();

        System.out.println( "The square of " + x + " is: " + power( x, 2 ) );

        System.out.println( "The cube of " + x + " is: " + power( x, 3 ) );

        System.out.println( "The hypercube of " + x +" is: " + power(x, 4));
    }
}

编辑: 我已经进行了更改但是我在第15,26和34行遇到了Stackoverflow错误。这是更改的代码。

public class Power{

    public static int square(int x){
        return x*x;
    }


    public static  int cube(int x){
        return power(x, 3);
    }

    public static int hypercube(int x){
        return power(x, 4);
    }




    public static int power(int x, int n){
        if (n==1){
            System.out.println(n);
        }

        if (n==2){
            square(x);
        }
        if (n==3){
            cube(x);
        }
        if (n==4){
            hypercube(x);
        }
        return x;


    }

    public static void main(String[] args){
        int x = 6;

        System.out.println( "The square of " + x +
                " is: " + power( x, 2 ) );

        System.out.println( "The cube of " + x +
                " is: " + power( x, 3 ) );

        System.out.println( "The hypercube of " + x +
                " is: " + power( x, 4 ) );
    }    
}

第15行是:

return power(x, 3);

第26行是:

if (n==1){

第34行是:

cube(x);

4 个答案:

答案 0 :(得分:3)

您无法从静态方法调用非静态方法。主要原因是除非已经为其创建了一个对象,否则不存在非静态方法。可以在没有该类实例的情况下调用静态方法。

Quick Explanation of Static Methods in Java

In-depth Explanation


修复代码中的所有问题

您的power()方法不会返回任何内容

你的方法调用中有一些奇怪的递归问题。您需要检查如何委派cube()hypercube()逻辑。


public class Power
{
   public static int square(int x)
   {
      return x*x;
   }

   public static int cube(int x)
   {
      return x*x*x;
   }

   public static int hypercube(int x)
   {
      return x*x*x*x;
   }

   public static int power(int x, int n)
   {
      if (n==2)
        return square(x);

      if(n==3)
        return cube(x);

      if(n==4)
        return hypercube(x);

      return x;
   }

   public static void main(String[] args)
   {
      int x = 6;

      System.out.println( "The square of " + x + " is: " + power(x, 2));

      System.out.println( "The cube of " + x + " is: " + power(x, 3));

      System.out.println( "The hypercube of " + x + " is: " + power(x, 4));
   }
}

为您学习任务!

这样可行,但是,您可以进一步明确改进此代码。想一想:如何重用 square()方法来计算cubehypercube

答案 1 :(得分:0)

使所有功能保持静止。

提示:所有不访问非静态字段或类功能的函数都可以是静态的。

在这种情况下,函数只使用它们的参数,因此可以变为静态。

答案 2 :(得分:0)

这取决于您的要求......

如果你提出它,你看起来更像是需要静态方法,而不是处理一个权力实例。

正如其他答案之一所述,如果您不访问任何实例变量,则应将方法设置为static

例如:

public static int power(int x, int n)

现在从main可以做到:

System.out.println( "The square of " + x + " is: " + Power.power( x, 2 ) );

并取消Power p = new Power();

如果确实要求您使用Power对象作为实例。

它将如此简单:

System.out.println("The square of " + x + " is: " + p.power(x, 2));

如果您正在尝试编写自己的数学函数。停止并使用java.lang.Math可以找到更多信息here

答案 3 :(得分:0)

是的,你可以。

以下面的代码为例:

class Cal {
    int cube(int x) {
        return x * x * x;
    }
    public static void main(String args[]) {
        //int result=Cal.cube(6);
        //Cal q=new cal();
        //q.cube(6);
        System.out.println(Cal.cube(6));
    }
}