简体   繁体   中英

How can I use methods from another class in a class?

When I run this code, I get the error "Cannot find symbol" on the 'getNum2' in the add method of the A class. How do I make it so that one class can use objects/methods from another class.

I think it would work if I placed that method in the main class, but I want to be able to call the "add" method for different objects and not have it hard coded for one set of values.

class Main {
  public static void main(String[] args) {
    System.out.println("Hello world!");
A a = new A(1);
B b = new B(2);
  }
}

public class A {

    private int num1;
    
    public A(int num){
      num1=num;
    }
    
    public int getNum1(){
      return num1;
    }
    
    public int add (){
      return getNum1()+getNum2();
    }
}

public class B{
    
    private int num2;
    
    public B (int num){
      num2=num;
    }
    
    public int getNum2(){
      return num2;
    }

}

Your add method is inside of A . That means you have access to A 's methods, such as getNum1 . If you want to call B 's methods inside of add , then you need access to an instance of B there. You can simply pass that instance as a parameter:

public int add (B b){
  return getNum1()+b.getNum2();
}

In your main you can then call it like

int sum = a.add(b);

To call getNum2() method inside Class A first you need to create an object of Class B inside the Class A or you need a reference to a Class B object.

Otherwise, you can you should specify getNum2() method as a static method.

  1. Create an Object of Class B inside Class A .
class A{

   private int num;

   int getNum1(){
       return num;
   }

   public int add(){
       B b = new B();
       return getNum1() + b.getNum2();
   }
}
  1. Specify getNum2() as static
class A{

    private int num;

    int getNum1(){

    }

    public int add(){
        return getNum1() + B.getNum2();
    }

}

class B{

    static int num;

    public static int getNum2(){
        return num;
    }
}

Please remember, terminologies I have used are in a very simple way as you can understand. Not in the best way. Also, I have skipped the constructors you have used to make this much more understandable.

You better study more on basic OOP concepts of Java. BTW, Good Luck!

This is the code that is more related to your code.

public class Main{
    public static void main(String[] args) {
        A a = new A(2);
        B b = new B(3);

        a.add(b);
    }   
}

class A{

    private int num;

    public A(int num){
        this.num = num;
    }

    int getNum1(){
        return num;
    }

    public int add(B b){
        return getNum1() + b.getNum2();
    }

}

class B{

    private int num;

    public B(int num){
        this.num = num;
    }

    public int getNum2(){
        return num;
    }
}

Your A class doesn't have a getNum2() method, nor does it have an instance of B.

Change the add method in A to:

public int add(int toAdd) {
  return num + toAdd;
}

and use it like this:

A a = new A(1);
B b = new B(2);
int result = a.add(b.getNum2());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM