在我的代码中,我希望获得另一个类中变量值的访问权限。 更确切地说,我希望在我的类GameArena中访问变量点的vallue,并用值覆盖我的GameActivity类中的变量点。 以下是课程:
public class GameArena extends View {
int points;
public GameArena(Context context) {
super(context);
[...]
points = 0;
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
[...]
invalidate();
}
public void actualizePoints(){
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
[...]
points++;
}
}
这是GameActivity:
public class GameActivity extends Activity {
Boolean gameRunning = false;
int round = 0;
int points = 0;
float screenScale;
GameArena gameArena;
FrameLayout gameArenaFrameLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
startGame();
}
public void startGame(){
startRound();
}
public void startRound(){
points = 0;
startGameArena();
}
public void startGameArena(){
gameArenaFrameLayout = (FrameLayout)findViewById(R.id.frameLayoutGameArena);
Log.i("GameArenaFrameLayout", Integer.toString(gameArenaFrameLayout.getWidth()));
gameArena = new GameArena(this);
gameArenaFrameLayout.addView(gameArena);
}
public void actualizeScreen(){
}
}
有人能给我一个提示,我该如何解决这个问题? 谢谢。
答案 0 :(得分:0)
使用getter:
public class GameArena extends View {
private int points;
public GameArena(Context context) {
super(context);
[...]
points = 0;
}
public int getPoints() {
return points;
}
}
活动:
gameArena.getPoints();