首页 > 解决方案 > 为什么传递参数时方法声明无效?

问题描述

    public class Store {
  // instance fields
  int area; 

  // constructor method
  public Calc(int one, int two, int three) {
    area = one*two*three;  
  }

  // main method
  public static void main(String[] args) {
    int sideOne = 2;
    int sideTwo = 3;
    int sideThree = 1;

    Calc mult = new Calc(sideOne,sideTwo,sideThree);

    System.out.println(mult.area);
  }
}

谁能帮助初学者理解为什么在传递参数时这是一个无效的方法声明?

标签: java

解决方案


您定义/调用Calc构造函数,但没有Calc类。

将您的类重命名为Calcant,您的代码将正确编译和执行:

public class Calc {
    // instance fields
    int area;

    // constructor method
    public Calc(int one, int two, int three) {
        area = one * two * three;
    }

    // main method
    public static void main(String[] args) {
        int sideOne = 2;
        int sideTwo = 3;
        int sideThree = 1;

        Calc mult = new Calc(sideOne, sideTwo, sideThree);

        System.out.println(mult.area);
    }
}

推荐阅读