【问题标题】:I want to add two numbers in A.java and print the result in B.java ( How? ) [closed]我想在 A.java 中添加两个数字并在 B.java 中打印结果(如何?)[关闭]
【发布时间】:2014-07-16 00:10:32
【问题描述】:

我想在 A.java 中添加两个数字并在 B.java 中打印结果(如何?)

A.java

class A {
   public static void main(String args[]) {
       int x= 5;
       int y = 8; 
   }

    void Sum() {
        z = x+y;
    }
}

B.java

class B {
    A obj = new A();
    System.out.println("Result= "A.Sum());
}

我无法解决这个问题。 请帮我 .. 先感谢您。

【问题讨论】:

标签: java object


【解决方案1】:

你想要更像这样的东西:

A.java

class A {
    int x;
    int y;
    public A (int x, int y) {
        this.x = x;
        this.y = y
    }

    public int sum() {
        return x + y;
    }
}

B.java

class B {
    public static void main(String[] args) {
        A obj = new A(5, 8);
        System.out.println("Result=" + obj.sum());
    }
}

由于您正在实例化A,它不需要main,它需要一个构造函数来设置值。它不需要像我写的那样接受参数,如果你愿意,你可以在构造函数中将它们设置为一些硬编码的值。 B 需要一个 main 方法,就像我假设你要运行它一样?如果没有,您可以将其更改为要从其他地方调用的构造函数。

【讨论】:

  • 感谢您的程序运行良好。但问题是我想在 main 函数中声明该方法并在 B.Java 中打印输出。 A.Java 将包含 main 函数和方法 Sum B.Java 将打印结果。
  • 我不确定你的意思。你想让sum() 方法在B.java 中,而main() 方法在A.java 中吗?
【解决方案2】:

这其实是很多新手很难掌握的概念。当你说new SomeClass() 时,你创建了一个类的“实例”,并返回了对该实例的“引用”,可以用作它的“句柄”来将访问从一个地方传递到另一个地方。

所以,在 A 的 main 你可能会这样做

A a = new A();
B b = new B();
b.doSomething(a);

在 B 中你可能有一个方法

public void doSomething(A aRef) {
    int result = aRef.sum();
    System.out.println("The answer is " + result);
}

然后回到 A,sum 方法

 public int sum() {
     return x + y;
 }

但是我们还没有定义或给 x 和 y 赋值,所以首先在 A 中插入任何方法之外的声明(通常在第一个方法之前)--

 int x;
 int y;

您可以在那里分配它们的值,但动态分配它们更“现实”。在main,在做new A之后但在调用doSomething之前

 a.x = 5;
 a.y = 8;

请注意,您必须提供a.,因为main 是静态的,因此不能自动寻址实例变量。

【讨论】:

    【解决方案3】:

    这里有很多错误。这是带有 cmets 的更正 A 类:

    class A {
    
        int x= 5;  //x and y must be declared here, as instance variables (not local variables in main)
        int y = 8; 
    
        public static void main(String args[]) { //you don't need a main method here
    
        }
    
        public int Sum() {  //you need to return an int here
            return(x+y);
        }
    }
    

    这是修正后的 B 类:

    class B {
        public static void main(String[] args) {
            A obj = new A();  //these two statements must be inside main()
                              //if you want them to execute
            System.out.println("Result= " + obj.Sum()); //use the object you created
                                                        //to call Sum()
        }
    }
    

    【讨论】:

      【解决方案4】:

      您的代码有两个主要问题

      1) 在 B 类中,您实例化 A 类的 obj,然后使用类 A.Sum() 这应该是

       A obj = new A();
       System.out.println("Result= " + obj.Sum());
      

      2) 在您的 A 类中,您的 main 方法中有变量 xy(未使用)。这些必须是类变量

      public class A{
      
      int x= 5;
      int y = 8; 
      
      public void Sum() 
      }
      

      此外,这些类的范围可能需要公开

      【讨论】:

        【解决方案5】:

        基本上OOP中的问题你可以这样解决

        A.java

        public class A {
        
            public static void main(String args[]) {
                new B().print();        
            }
        
            public static int sum(int... values) {
                int total=0;
                for(int num: values){
                    total+=num;
                }
                return total;
            }
        }
        

        B.java

        public class B {
        
            public void print(){
                System.out.println("Result= "+A.sum(4,2));
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-08-03
          • 1970-01-01
          • 2015-09-26
          • 2022-07-29
          • 1970-01-01
          • 1970-01-01
          • 2015-04-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多