简体   繁体   English

Java:如何从另一个 class 访问方法

[英]Java: How to access methods from another class

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?我的问题是如何从 Alpha 中的方法访问 cBeta.DoSomethingBeta()?

You need to somehow give class Alpha a reference to cBeta.您需要以某种方式为 class Alpha 提供对 cBeta 的引用。 There are three ways of doing this.有三种方法可以做到这一点。

1) Give Alphas a Beta in the constructor. 1) 在构造函数中给 Alphas 一个 Beta。 In class Alpha write:在 class Alpha 中写入:

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

and call cAlpha = new Alpha(cBeta) from main()并从 main() 调用 cAlpha = new Alpha(cBeta)

2) give Alphas a mutator that gives them a beta. 2)给Alphas一个mutator,给他们一个beta。 In class Alpha write:在 class Alpha 中写入:

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

and call cAlpha = new Alpha();并调用 cAlpha = new Alpha(); cAlpha.setBeta(beta); cAlpha.setBeta(beta); from main(), or从 main(),或

3) have a beta as an argument to doSomethingAlpha. 3) 有一个 beta 作为 doSomethingAlpha 的参数。 in class Alpha write:在 class 阿尔法写:

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.如果您希望每个 Alpha 都有一个 Beta,请使用数字 1。如果您希望只有一些 Alpha 拥有一个 Beta,但您希望他们无限期地持有他们的 Beta,请使用数字 2。如果您希望 Alpha 只处理 Beta当您调用 doSomethingAlpha 时,请使用数字 3。变量 scope 起初很复杂,但当您掌握它时会变得更容易。 Let me know if you have any more questions!如果您还有其他问题,请告诉我!

Method 1:方法一:

If the method DoSomethingBeta was static you need only call:如果 DoSomethingBeta 方法是 static 你只需要调用:

Beta.DoSomethingBeta();

Method 2:方法二:

If Alpha extends from Beta you could call DoSomethingBeta() directly.如果 Alpha 从 Beta 扩展而来,您可以直接调用 DoSomethingBeta()。

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.或者,您需要访问 Beta 实例才能从中调用方法。

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

Incidentally is this homework?顺便说一句,这是作业吗?

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另一个 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());

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.如果 Alpha 和 Beta 是您唯一的额外 class 那么为什么不使用 class 的图像创建一个 static 变量。

Like in Alpha class:就像在阿尔法 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:现在您可以使用以下几行代码在 Beta class 中调用 function:

new Alpha();
Alpha.alpha.DoSomethingAlpha();

You either need to create an object of type Beta in the Alpha class or its method您需要在 Alpha class 或其方法中创建一个 Beta 类型的 object

Like you do here in the Main Beta cBeta = new Beta();就像您在 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如果要使用在 Main 中创建的变量,则必须通过使 Alpha 构造函数看起来像将其解析为 cAlpha 作为参数

public class Alpha 
{

    Beta localInstance;

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


     public void DoSomethingAlpha() 
     {
          localInstance.DoSomethingAlpha();     
     }
}

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());   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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