gpt4 book ai didi

java - 'Static' 关键字有什么作用?

转载 作者:行者123 更新时间:2023-12-01 06:56:41 27 4
gpt4 key购买 nike

我有一个程序,其中有两个形状 Player1 和 Player2,均由 Player 类继承。

Public class Player{

public int xPos;
public int yPos;

// more code

}

player1 类:

Public class Player1 extends Player{

public Player(){
xPos = 200;
yPos = 200;
}
// more code
}

Player2 类:

Public class Player2 extends Player{

public Player2(){

xPos = 400;
yPos = 200;
}
// more code
}

在这种情况下,我应该对 Player 类中的 xPos 和 yPos 使用 static 吗?

最佳答案

如果 xPos 和 yPos 是静态的,则 Player 的每个实例都将具有相同的位置。

关于java - 'Static' 关键字有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com