[英]How can I have access to one item's variables in another class that is not related with it?
我是java的新手,並且我已經搜索了很長時間。 如何在Player類中訪問項目的b變量?(我發布的鱈魚是我完整程序的一部分,因此請不要介意是否看到以下代碼中未聲明的方法或變量)
import java.util.Random;
public abstract class Player {
private int x, y;
private String name;
private int pNumber;
private int mLine;
private int tLine;
private boolean possession;
private int c;
private int f = 0;
private int i = 0;
public int getPlx() {
return x;
}
public void setPlx(int x) {
this.x = x;
}
public int getPly() {
return y;
}
public void setPly(int y) {
this.y = y;
}
public String getPName() {
return name;
}
public void setPName(String name) {
this.name = name;
}
public int getPNum() {
return pNumber;
}
public void setPNum(int pNumber) {
this.pNumber = pNumber;
}
public int getMLine() {
return mLine;
}
public void setMLine(int mLine) {
this.mLine = mLine;
}
public int getLine() {
return tLine;
}
public void setTLine(int tLine) {
this.tLine = tLine;
}
public boolean getPos() {
return possession;
}
public void setPos(boolean possession) {
this.possession = possession;
}
private Random rand = new Random();
public void Move() { //me8odos metakinisis
c = rand.nextInt(2);
if (c == 0) {
y++;
} else {
y--;
}
}
public void Pass() {
if (this.possession == true) {
c = rand.nextInt(10);
while ((f == 0) && (i < 10)) {
if (main.barcelona.get(i).name == this.name) {}
}
}
}
public abstract void SpecialMove();
}
public class Ball {
private int x, y;
private Player formerP = null;
private Player currentP = null;
public Ball(int x, int y, Player formerP, Player currentP) {
this.x = x;
this.y = y;
this.formerP = formerP;
this.currentP = currentP;
}
public int getBX() {
return x;
}
public void setBX(int x) {
this.x = x;
}
public int getBY() {
return y;
}
public void setBY(int y) {
this.y = y;
}
void Assign(Player playerP) {
int px = playerP.getPlx();
if (this.currentP == null) {
if (((this.x - px <= 1) || (px - this.x) <= 1)
&& ((this.x - px <= 1) || (px - this.x) = 1)) {
this.currentP = playerP;
this.formerP.possession = false;
playerP.possession = true;
if (this.currentP.team == this.formerP.team) {
int pass = this.currentP.getPasses();
pass++;
this.currentP.setPasses(pass);
} else {
int mistake = this.currentP.getMistakes();
mistake++;
this.currentP.setMistakes(mistake);
}
}
}
this.formerP = this.currentP;
this.currentP = null;
this.formerP = null;
}
}
如果您不想在Player
類中實例化Ball
類, 並且想確保所有玩家都在玩一個唯一的球,請考慮將Ball
變成Singleton 。
在軟件工程中,單例模式是一種設計模式,用於將類的實例化限制為一個對象。 當僅需要一個對象來協調整個系統中的動作時,這將很有用。
public class Ball {
private static Ball singletonBall;
private int x;
private int y;
private Player formerP;
private Player currentP;
public static Ball getSingletonBall() {
if(singletonBall == null){
singletonBall = new Ball();
}
return singletonBall;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Player getFormerP() {
return formerP;
}
public void setFormerP(Player formerP) {
this.formerP = formerP;
}
public Player getCurrentP() {
return currentP;
}
public void setCurrentP(Player currentP) {
this.currentP = currentP;
}
}
如果在Player內部需要經常使用的東西在Player對象(或子對象)的整個生命周期中都不會改變,那么您最好將其通過Player傳遞給局部變量構造函數。
假設項目B是Ball,並且您擁有Ball類。
因此,在播放器(或子播放器)中聲明要訪問的對象:
class Player {
Ball ball;
public Player(Ball ball) {
this.ball = ball;
}
或者,如果只是不經常使用的東西,或者有價值的東西會改變(更可能是球),則可以創建一種方法來執行Player對象上所需的操作。
class Player {
.
.
.
.
.
public void dribble(Ball ball) {
// do something with the ball
ball.setPosition(10, 20);
ball.update();
}
}
然后,任何實例化的Player都可以訪問該方法。
public static void main(String[] args) {
Player player = new Player();
Ball ball = new Ball();
player.dribble(ball);
}
嘗試BallClassName.getX(); 您可能必須使getX靜態
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.