Вопрос:

Java: How to access methods from another class

java

263464 просмотра

6 ответа

167 Репутация автора

I tried to simplify my predicament as much as possible. I have three classes:

Alpha:

public class Alpha {
     public void DoSomethingAlpha() {
          cbeta.DoSomethingBeta()  //?
     }
}

Beta:

public class Beta {
     public void DoSomethingBeta() {
          // Something
     }
}  

Main:

public class MainApp {
     public static void main(String[] args) {           
          Alpha cAlpha = new Alpha();   
          Beta cBeta = new Beta();
     }
}

I hope I did not over simplify it. My question is how do I access cBeta.DoSomethingBeta() from a method in Alpha?

Автор: co757 Источник Размещён: 04.07.2011 11:35

Ответы (6)


0 плюса

1834 Репутация автора

You either need to create an object of type Beta in the Alpha class or its method

Like you do here in the Main Beta cBeta = new Beta();

If you want to use the variable you create in your Main then you have to parse it to cAlpha as a parameter by making the Alpha constructor look like

public class Alpha 
{

    Beta localInstance;

    public Alpha(Beta _beta)
    {
        localInstance = _beta;
    }


     public void DoSomethingAlpha() 
     {
          localInstance.DoSomethingAlpha();     
     }
}
Автор: Mech0z Размещён: 04.07.2011 11:45

0 плюса

905 Репутация автора

Maybe you need some dependency injection

public class Alpha {

    private Beta cbeta;

    public Alpha(Beta beta) {
        this.cbeta = beta;
    }

    public void DoSomethingAlpha() {
        this.cbeta.DoSomethingBeta();
    }
}

and then

Alpha cAlpha = new Alpha(new Beta());   
Автор: Felipe Cruz Размещён: 04.07.2011 11:48

18 плюса

10457 Репутация автора

Method 1:

If the method DoSomethingBeta was static you need only call:

Beta.DoSomethingBeta();

Method 2:

If Alpha extends from Beta you could call DoSomethingBeta() directly.

public class Alpha extends Beta{
     public void DoSomethingAlpha() {
          DoSomethingBeta();  //?
     }
}

Method 3:

Alternatively you need to have access to an instance of Beta to call the methods from it.

public class Alpha {
     public void DoSomethingAlpha() {
          Beta cbeta = new Beta();
          cbeta.DoSomethingBeta();  //?
     }
}

Incidentally is this homework?

Автор: Mark McLaren Размещён: 04.07.2011 11:49

31 плюса

854 Репутация автора

Решение

You need to somehow give class Alpha a reference to cBeta. There are three ways of doing this.

1) Give Alphas a Beta in the constructor. In class Alpha write:

public class Alpha {
   private Beta beta;
   public Alpha(Beta beta) {
     this.beta = beta; 
   }

and call cAlpha = new Alpha(cBeta) from main()

2) give Alphas a mutator that gives them a beta. In class Alpha write:

public class Alpha {
   private Beta beta;
   public void setBeta (Beta newBeta) {
     this.beta = beta;
   }

and call cAlpha = new Alpha(); cAlpha.setBeta(beta); from main(), or

3) have a beta as an argument to doSomethingAlpha. in class Alpha write:

public void DoSomethingAlpha(Beta cBeta) {
      cbeta.DoSomethingBeta()
}

Which strategy you use depends on a few things. If you want every single Alpha to have a Beta, use number 1. If you want only some Alphas to have a Beta, but you want them to hold onto their Betas indefinitely, use number 2. If you want Alphas to deal with Betas only while you're calling doSomethingAlpha, use number 3. Variable scope is complicated at first, but it gets easier when you get the hang of it. Let me know if you have any more questions!

Автор: krasnerocalypse Размещён: 04.07.2011 11:54

0 плюса

6323 Репутация автора

public class WeatherResponse {

private int cod;
private String base;
private Weather main;

public int getCod(){
    return this.cod;
}

public void setCod(int cod){
    this.cod = cod;
}

public String getBase(){
    return base;
}

public void setBase(String base){
    this.base = base;
}

public Weather getWeather() {
    return main;
}

// default constructor, getters and setters
}

another class

public class Weather {

private int id;
private String main;
private String description;

public String getMain(){
    return main;
}

public void setMain(String main){
    this.main = main;
}

public String getDescription(){
    return description;
}

public void setDescription(String description){
    this.description = description;
}

// default constructor, getters and setters
}

// accessing methods
// success!

    Log.i("App", weatherResponse.getBase());
    Log.i("App", weatherResponse.getWeather().getMain());
    Log.i("App", weatherResponse.getWeather().getDescription());
Автор: Vinod Joshi Размещён: 03.06.2015 11:36

0 плюса

1276 Репутация автора

I have another solution. If Alpha and Beta are your only extra class then why not make a static variable with the image of the class.

Like in Alpha class :

public class Alpha{
        public static Alpha alpha;
        public Alpha(){
                this.alpha = this;
}

Now you you can call the function in Beta class by just using these lines :

new Alpha();
Alpha.alpha.DoSomethingAlpha();
Автор: arqam Размещён: 23.08.2016 05:59
Вопросы из категории :
32x32