从另一个类访问变量?

时间:2017-08-24 20:32:37

标签: java android

我有一个名为BrickBreaker的主游戏类

public class BrickBreaker extends Activity {
  // there is lot of other code but i am only pointing to the issue
  class BreakoutView extends SurfaceView implements Runnable {
    // The size of the screen in pixels
    int screenX;
    int screenY;

    // Get a Display object to access screen details
        Display display = getWindowManager().getDefaultDisplay();
        // Load the resolution into a Point object
        Point size = new Point();
        display.getSize(size);

        screenX = size.x;
        screenY = size.y;
}}

从另一个班级(下方)我想从Main课程中访问screenX

public class Paddle {

  // This the the constructor method
// When we create an object from this class we will pass
// in the screen width and height
public Paddle(int screenX, int screenY){
    // 130 pixels wide and 20 pixels high
    length = 130;
    height = 20;

    // Start paddle in roughly the sceen centre
    x = screenX / 2;
    y = screenY - 20;

    rect = new RectF(x, y, x + length, y + height);

    // How fast is the paddle in pixels per second
    paddleSpeed = 550;

}
  public void update(long fps){
    rect.left = x;
      rect.right = x + length;
      if (x<0){
         x=0;
      }
      else if (x+length > screenX){
         x = screenX-length;
      }
   }
}

如何从screenX课程中访问Paddle

2 个答案:

答案 0 :(得分:1)

您需要从screenX课程中访问BreakoutView的成员Paddle

为此,您的Paddle类首先需要访问BreakoutView实例。

您可以通过在构造函数中传递它来执行此操作:

public class Paddle {
  BreakoutView view;
  public Paddle (BreakoutView view) {
    this.view = view;
  }

  public void update(long fps){
    ...
  }
}

在哪里创建它:

BreakoutView view = new BreakoutView(.....);
Paddle paddle = new Paddle(view);
....

接下来,您需要访问screenX成员。有两种选择:

首先:公开:

// The size of the screen in pixels
public int screenX;
public int screenY;

使用此解决方案,您可以使用Paddlethis.view.screenX课程中访问该解决方案。

但是这将允许其他代码(例如来自Paddle类内部)修改这些值,这通常是不希望的。因此,更安全的方式是:

第二:提供一个吸气剂。

在BreakoutView中,添加以下方法:

public int getScreenX() {
  return this.screenX;
}

然后,在Paddle课程中,您可以像this.view.getScreenX()一样访问它,而不仅仅是screenX

答案 1 :(得分:1)

在Paddle类修改字段screenX

Java静态变量

如果将任何变量声明为静态,则称为静态变量。

静态变量可用于引用所有对象的公共属性(对于每个对象而言不是唯一的),例如公司员工姓名,学生姓名等 静态变量在类加载时仅在类区域中获取一次内存。 静态变量的优点

请检查:STATIC

它使程序存储器有效(即节省内存)。

public class Paddle {

public static int screenX;

  // This the the constructor method
// When we create an object from this class we will pass
// in the screen width and height
public Paddle(int screenX, int screenY){
this.screenX = screenX;
    // 130 pixels wide and 20 pixels high
    length = 130;
    height = 20;

    // Start paddle in roughly the sceen centre
    x = screenX / 2;
    y = screenY - 20;

    rect = new RectF(x, y, x + length, y + height);

    // How fast is the paddle in pixels per second
    paddleSpeed = 550;

}
  public void update(long fps){
    rect.left = x;
      rect.right = x + length;
      if (x<0){
         x=0;
      }
      else if (x+length > screenX){
         x = screenX-length;
      }
   }
}

要在BrickBeaker中访问,您需要:

Paddle.screenX

享受