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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6576855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:51:44  来源:igfitidea点击:

Java: How to access methods from another class

java

提问by co757

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

采纳答案by krasnerocalypse

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

您需要以某种方式为 Alpha 类提供对 cBeta 的引用。有三种方法可以做到这一点。

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

1) 在构造函数中给 Alphas 一个 Beta。在 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. In class Alpha write:

2)给Alphas一个mutator,给他们一个beta。在 Alpha 课上写:

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

并调用 cAlpha = new Alpha(); cAlpha.setBeta(beta); 从主(),或

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

3) 有一个 beta 作为 doSomethingAlpha 的参数。在 Alpha 课上写:

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!

您使用哪种策略取决于一些事情。如果您希望每个 Alpha 都有 Beta,请使用编号 1。如果您只希望某些 Alpha 拥有 Beta,但希望他们无限期地保留其 Beta,请使用编号 2。如果您希望 Alpha 只处理 Beta当你调用 doSomethingAlpha 时,使用数字 3。变量作用域一开始很复杂,但是当你掌握它的窍门时它会变得更容易。如果您还有其他问题,请告诉我!

回答by Mech0z

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

您需要在 Alpha 类或其方法中创建一个 Beta 类型的对象

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

回答by Felipe Cruz

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

回答by Mark McLaren

Method 1:

方法一:

If the method DoSomethingBeta was static you need only call:

如果方法 DoSomethingBeta 是静态的,则只需调用:

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?

顺便问一下这是作业吗?

回答by Vinod Joshi

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

回答by arqam

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 是您唯一的额外类,那么为什么不使用类的图像创建一个静态变量。

Like in Alpha class :

就像在 Alpha 课上:

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 类中调用该函数:

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