java中如何在方法之间传递值

问题描述 投票:0回答:1

我是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 methods
1个回答
0
投票

“...我想确认如何将值从方法传递到另一个方法/类。...”

实现这一目标的方法不止一种。

我建议使用参数返回值。
定义方法(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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.