简体   繁体   English

调用在另一个类中创建的对象的方法

[英]Call a method of the object which is created in another class

I have a little problem. 我有一点问题。

I have a 3 classes looks "like" this: 我有一个3类,看起来像“这样”:

public class Gun{
    public void Shot(){
        // shoot code
    }

    public void RenderGun()
    {
        // render code
    }

    // more methods
}


public class GameManager
{
    Gun gun;

    public GameManager()
    {
        gun = new Gun();
    }

    // methods using GunClass methods.
}

public class HudScreen
{
    Gun gun;

    public HudScreen()
    {
        gun = new Gun();
    }

    public void GunShotButton()
    {
        gun.Shoot();
    }
}

The problem is I have two Gun objects and i want to have one but use it in both classes HudScreen and GameManager. 问题是我有两个Gun对象,我想拥有一个,但是在HudScreen和GameManager类中都使用了它。

So, if I create a new Gun object in GameManager, how to "call" this object from the HudScreen class? 因此,如果我在GameManager中创建一个新的Gun对象,如何从HudScreen类中“调用”该对象?


Thanks, MM. 谢谢,MM。

Change constructors in that way: 以这种方式更改构造函数:

public GameManager(Gun gun) {
   this.gun = gun;
}

and the same in HudScreen class. HudScreen类中的相同。 Then in some Main class: 然后在一些Main class中:

class Main {    
   public static void main(String[] args) {
      Gun gun = new Gun();
      HudScreen hudScreen = new HudScreen(gun);
      GameManager gameManager = new GameManager(gun)
   }
}

Currently main method is assumed to be the top level method doing the initilization. 当前, main方法被假定为进行初始化的顶层方法。 This may be some other method as well where objects of Gun , GunManager and HudScreen are created. 在创建GunGunManagerHudScreen对象时,也可以使用其他方法。

GameManager constructor GameManager构造函数

 public GameManager(Gun gun){ 
     this.gun = gun;
 }

HudScreen constructor HudScreen构造函数

public HudScreen(Gun gun){
      this.gun = gun;
 }

From the main method where you create the Gun object you can pass it to the constructors of GameManager and HudScreen 在创建Gun对象的主要方法中,可以将其传递给GameManager和HudScreen的构造函数

 public static void main() {
      Gun gun = new Gun();
      HudScreen hudScreen = new HudScreen(gun);
      GameManager gameManager = new GameManager(gun)
   }

If you do not want to create GameManager and HudScreen in same method but still want to use same Gun object then you can do it this way 1. Initialize Gun object 2. on the GameManager and Hudscreen you can either pass it on constructor or you can directly pass it to method in theses classes. 如果您不想以相同的方法创建GameManager和HudScreen,但仍想使用相同的Gun对象,则可以按以下方式进行操作:1.初始化Gun对象2.在GameManager和Hudscreen上,您可以将其传递给构造函数,也可以直接将其传递给这些类中的方法。 eg Assume you want to keep the GameManager constructor receive the newly created Gun and pass the same gun to Hudscreen.GunShotButton. 例如,假设您想让GameManager构造函数接收新创建的Gun,并将相同的Gun传递给Hudscreen.GunShotButton。 I hope you got a clue here - 希望您对此有所了解-

public class Gun{
    public void Shot(){
        // shoot code
    }

    public void RenderGun()
    {
        // render code
    }

    // more methods
}


public class GameManager
{
    Gun gun;

    public GameManager(Gun gun)
    {
        this.gun =gun;
    }

    // methods using GunClass methods.
}

public class HudScreen
{   
    public HudScreen()
    {
    }

    public void GunShotButton(Gun gun)
    {
        gun.Shoot();
    }
}

register a ScreenController to every NiftyGUI screen. 向每个NiftyGUI屏幕注册一个ScreenController。 create a ScreenController by creating a Java class that implements the de.lessvoid.nifty.screen.ScreenController interface and its abstract methods. 通过创建实现de.lessvoid.nifty.screen.ScreenController接口及其抽象方法的Java类,创建ScreenController。

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

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