java - 为什么传递参数时方法声明无效?
问题描述
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);
}
}
谁能帮助初学者理解为什么在传递参数时这是一个无效的方法声明?
解决方案
您定义/调用Calc
构造函数,但没有Calc
类。
将您的类重命名为Calc
ant,您的代码将正确编译和执行:
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);
}
}
推荐阅读
- linux - 随机查找适用于空格的结果
- html - HTML noob 试图将编号列表中的数字涂成红色
- kotlin - 前台服务未在后台更新传感器数据
- react-native - how can I change the text color if it focused?
- powerbi - PowerBI如何将2个表中的值动态组合成一个URL?
- css - 如何将移动元素定位在父元素后面
- matplotlib - 您可以将 Seaborn 参数应用于使用 matplotlib 创建的 3d 函数热图吗?
- javascript - 为什么我在 javascript 中用于 google sheet 2d 的数组不是?
- flask - Flask-SocketIO 处理程序在空闲一段时间后无法工作
- tensorflow - Tensorflow 废话重塑价值观