【发布时间】:2017-10-29 18:36:27
【问题描述】:
假设我在类 1 中有两个方法。我可以将参数传递给类 1 构造函数,然后将参数传递给这两个方法吗?类似于下面的示例代码:
class stuff{
int c;
stuff(x){
c = x;
}
public static int sum(int a, int b){
stuff self = new stuff();
return c*(a + b);
}
public static int mult(int a, int b){
return c*(a*b);
}
}
class test{
public static void main(String args[]){
stuff foo = new stuff(5);
System.out.println(stuff.sum(1, 2));
System.out.println(stuff.mult(1, 2));
}
}
所以从类测试中我想从类的东西中访问这两个方法,同时传递方法的参数,但我还想传递一个全局类参数(在这种情况下为 5)。我该怎么做?
【问题讨论】:
-
你已经按照你的描述做了。你试过了吗?尝试时有什么问题?我建议你为 Java 类做一些教程。
-
所以你的意思是有一个变量 c 的 getter 方法?
标签: java class methods constructor