我是java新手,我想确认如何将值从方法传递到另一个方法/类。
例如。
在
package1
public class class1
public void method1() {
a + a = e
}
在
package1
但在不同的 class file
public class class2
public void method2() {
b + b = f
}
在
package2
public void method3() {
c + c = g
}
在
package3
public void method4() {
// how can I get or use the value produce from method 1 , 2 and 3?
(e) + (f) + (g) = d
}
注意:我使用 Eclipse 作为 IDE。
“...我想确认如何将值从方法传递到另一个方法/类。...”
实现这一目标的方法不止一种。
我建议使用参数和返回值。
定义方法(Java™ 教程 > 学习 Java 语言 > 类和对象)。
class A {
static int f(int a) {
return a + a;
}
}
class B {
static int f(int b) {
return b + b;
}
}
class C {
static int f(int c) {
return c + c;
}
}
class D {
static int f() {
return A.f(1) + B.f(2) + C.f(3);
}
}