有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

类访问跨多个JAVA类的变量

我是JAVA新手,正在编写一个在两个不同类之间传递变量的程序。我似乎不知道如何将变量的数据从一个类发送到另一个类,然后再发送回来?这可能吗

例如:

Public Class A {
    Scanner input = new Scanner(System.in);
    System.out.println("Please input the weight:");
            bagWeight=input.nextDouble();
    System.out.println("The total weight is: "+total);
}

Public Class B {
    double number = 3;
    public method(double number, double bagWeight) 
    {
        total = bagWeight*number;
        return total;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    当然这是可能的:有很多方法可以做到这一点

    例如,您可以轻松做到这一点:

    //Obviously pseudo Code...u need a constructor..or method
    Public Class A{
                Scanner input = new Scanner(System.in);
                System.out.println("Please input the weight:");
                bagWeight=input.nextDouble();
                B b = new B();
                double total = b.method(1337, bagWheight);
                System.out.println("The total weight is: "+total);
        }