如何使用两个类获取两个变量的和。

huangapple 未分类评论46阅读模式
英文:

How to get the sum of two variables using two classes

问题

从Rational Number类中的代码:

public class CameronH51 { 

      private int numerator1, denominator1;
   
      public void add()

    {
   
    System.out.print(numerator1 + denominator1);
    
    }
   


    }

从CameronH51类的代码:

public class RationalNumber 
    {

    int numerator1 = 3 , denominator1 = 6;

    public static void main(String[] args)
   
    {
   
    CameronH51 object = new CameronH51();
   
    object.add();
  
    }

    }
英文:

How do I connect these two classes, so that class RationalNumber can get the calculation from class CameronH51?

Code From Rational Number:

public class CameronH51 { 

      private int numerator1, denominator1;
   
      public void add()

    {
   
    System.out.print(numerator1 + denominator1);
    
    }
   

    }

Code From CameronH51:

public class RationalNumber 
    {

    int numerator1 = 3 , denominator1 = 6;

    public static void main(String[] args)
   
    {
   
    CameronH51 object = new add();
   
    object.add();
  
    }

    }

答案1

得分: 0

请尝试这样做:

  • 创建一个有理数实例以访问字段以进行加法操作。
  • 创建一个CameronH51实例以调用add方法。
  • 打印结果。
public class RationalNumber {
	
	int numerator1 = 3, denominator1 = 6;
	public static void main(String[] args) {
		RationalNumber rn = new RationalNumber();
		CameronH51 cameronInstance = new CameronH51();
		
		int result = cameronInstance.add(rn.numerator1,rn.denominator1);
		System.out.println("result = " + result);
	}
	
}

class CameronH51 {
	// add two numbers and return result
	public int add(int a, int b) {
		return a + b;
	}
}
英文:

Try this:

  • create an instance of RationalNumber to access the fields to add
  • create an instane of CameronH51 to call the add method
  • print the result.
public class RationalNumber {
	
	int numerator1 = 3, denominator1 = 6;
	public static void main(String[] args) {
		RationalNumber rn = new RationalNumber();
		CameronH51 cameronInstance = new CameronH51();
		
		int result = cameronInstance.add(rn.numerator1,rn.denominator1);
		System.out.println("result = " + result);
	}
	
}


class CameronH51 {
	// add two numbers and return result
	public int add(int a, int b) {
		return a + b;
	}
}


</details>



# 答案2
**得分**: 0

你可以简单地使用以下示例中的静态方法:

```java
public class A {

    public static double add(double number1, double number2){
        return number1+number2;
    }
}

然后在另一个类中使用它:

public class B {

    public static void main(String[] args) {
        double result = A.add(5, 8);
        System.out.println(result);
    }
}
英文:

You can simply use static methods like the samples below:

public class A {

    public static double add(double number1, double number2){
        return number1+number2;
    }
}

and then use it in the other class:

public class B {

    public static void main(String[] args) {
        double result = A.add(5, 8);
        System.out.println(result);
    }
}

答案3

得分: 0

你需要将这两个数字放入你的CameronH51类中。最常见的方法是添加一个构造函数来接收这两个值:

public class CameronH51 { 
    
  private int numerator1, denominator1;
   
  public CameronH51(int num, int den) {
    numerator1 = num;
    denominator1 = den;
  }

  public void add() {  
    System.out.println(numerator1 + denominator1);
  }

}

现在,在RationalNumber类中,你可以创建自己的Add()方法。在这个方法中,你会创建一个CameronH51的实例(通过构造函数从RationalNumber传入值),并调用Add()方法:

public class RationalNumber {

  private int numerator1 = 3;
  private int denominator1 = 6;

  public void add() {
    CameronH51 c = new CameronH51(numerator1, denominator1);
    c.add();
  }

}

这是一个包含main()方法将所有内容联系在一起的类:

class Main {

  public static void main(String[] args) {
    RationalNumber rn = new RationalNumber();
    rn.add();
  }

}

这是一个链接到Repl.it的链接,你可以在上面进行操作。

话虽如此,这两个类的设计似乎有点不太合理。如果没有更好的描述,我们无法确定你是否在错误的方向上解决问题。

英文:

You need to get the two numbers INTO your CameronH51 class. The most common way to do this would be to add a CONSTRUCTOR that receives the two values:

public class CameronH51 { 

  private int numerator1, denominator1;
   
  public CameronH51(int num, int den) {
    numerator1 = num;
    denominator1 = den;
  }

  public void add() {  
    System.out.println(numerator1 + denominator1);
  }

}

Now, over in RationalNumber, you can create its own Add() method. In that method you'd create an instance of CameronH51 (passing in the values from RationalNumber via the Constructor), and call the Add() method:

public class RationalNumber {

  private int numerator1 = 3;
  private int denominator1 = 6;

  public void add() {
    CameronH51 c = new CameronH51(numerator1, denominator1);
    c.add();
  }

}

Here's a class with main() tying everything together:

class Main {

  public static void main(String[] args) {
    RationalNumber rn = new RationalNumber();
    rn.add();
  }

}

Here's a link to a Repl.it to play with.

With all that said, the design of the two classes seems a little off. You may be approaching the problem wrong, but we don't know that without a better description...

huangapple
  • 本文由 发表于 2020年7月24日 22:16:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/63075445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定