我有一个超类Player
,它有ArrayList<Card> cards
和ArrayList<Card> cardsOnTable
。我还有一个类Bot
,它扩展了Player
类。我的Card
类具有used
布尔属性。我在Player
类上有一个方法,可以返回未使用的卡片。
我的问题是,当我在一个Bot对象实例之一中设置我的卡时,cards <ArrayList>
在其他实例中也会更改。为什么会这样?我对继承有误吗?
这是我的课程:
public class Bot extends Player{
private int id;
private String name; //added to print player at the end of war (I didn't know what to write) -Gonca
private int warPoints; // war points
private int boardNum; // for connecting with board city
private int score;
private Card lastCardPlayed;
public Bot() {
super();
this.id = 0;
this.name = "Bot " + (id + 1); //if not set, then it is a Bot and it has only number
lastCardPlayed = new Card();
}
public Bot(int id) {
super();
this.id = id;
this.name = "Bot " + (this.id + 1); //if not set, then it is a Bot and it has only number
lastCardPlayed = new Card();
}
public Bot(int id, City city) {
super(id,city);
this.id = id;
this.name = "Bot " + (id + 1); //if not set, then it is a Bot and it has only number
lastCardPlayed = new Card();
System.out.println("Bot constructor with City parameter");
}
public class Player {
private int id;
private String name; //
private int warPoints; // war points
private ArrayList<Card> cards; // cards in hand
private ArrayList<Card> cardsOnTable;
private int boardNum; // for connecting with board city
private int score;
private City city;
private ArrayList<Material> resources;
int numberOfCoin;
int numberOfClay;
int numberOfOre;
int numberOfStone;
int numberOfWood;
int numberOfLoom;
int numberOfGlass;
int numberOfPapyrus;
int numberOfMilitary;
int numberOfCivilian;
int numberOfScienceRuler;
int numberOfScienceStone;
int numberOfScienceWheel;
public Player() {
this.id = 0;
this.name = "Player " + (id + 1);
this.cards = new ArrayList<>();
this.cardsOnTable = new ArrayList<>();;
this.city = new City();
this.resources = new ArrayList<>();
this.score = 0;
this.numberOfCoin = 3;
}
public Player(int id, City city) {
this.id = id;
this.city = city;
this.name = "Player " + (id + 1);
this.cards = new ArrayList<>();
this.cardsOnTable = new ArrayList<>();;
this.resources = new ArrayList<>();
this.score = 0;
this.numberOfCoin = 3;
}
public ArrayList<Card> getCards() {
ArrayList<Card> temp = new ArrayList<>();
for(int i = 0; i < cards.size(); i++)
{
if( !(cards.get(i).isUsed()))
temp.add(cards.get(i));
}
return temp;
}
public String addCardsToTable(Card c) {
if(verifySufficientResources(c)){
c.setUsed(true);
cardsOnTable.add(c);
return "";
}else{
return ("Resources not enough");
}
}
public class Card {
private String name; // name
private ArrayList<Material> requirements; // materials for playing this card.
private ArrayList<Material> earnings; // come from card materials
private int Id; // special card id for implementing card chain
private int nextCardId; // id of the free card from card chain.
private String color; // card color
private String photoName; // for photo directory
private int ageNumber; // age number of card.
private boolean orSituation;
private String specialFunctionName;
private boolean used;
private int cardWarPoint; // bunu sonradan ekledim aşağıdakilere göre eklemek lazım- efe
public Card(String name, ArrayList<Material> requirements, ArrayList<Material> earnings, int Id, int nextCardId, String color, String photoName, int ageNumber) {
this.name = name;
this.requirements = requirements;
this.earnings = earnings;
this.Id = Id;
this.nextCardId = nextCardId;
this.color = color;
this.photoName = photoName;
this.ageNumber = ageNumber;
this.orSituation = false;
this.specialFunctionName = "none";
this.used = false;
}
public Card(String name, ArrayList<Material> requirements, ArrayList<Material> earnings, int Id, int nextCardId, String color, String photoName, int ageNumber, boolean orSituation) {
this.name = name;
this.requirements = requirements;
this.earnings = earnings;
this.Id = Id;
this.nextCardId = nextCardId;
this.color = color;
this.photoName = photoName;
this.ageNumber = ageNumber;
this.orSituation = orSituation;
this.specialFunctionName = "none";
this.used = false;
}
public Card(String name, ArrayList<Material> requirements, ArrayList<Material> earnings, int Id, int nextCardId, String color, String photoName, int ageNumber, String specialFunctionName) {
this.name = name;
this.requirements = requirements;
this.earnings = earnings;
this.Id = Id;
this.nextCardId = nextCardId;
this.color = color;
this.photoName = photoName;
this.ageNumber = ageNumber;
this.orSituation = false;
this.specialFunctionName = specialFunctionName;
this.used = false;
}
public Card(String name, ArrayList<Material> requirements, ArrayList<Material> earnings, int Id, int nextCardId, String color, String photoName, int ageNumber, boolean orSituation, String specialFunctionName) {
this.name = name;
this.requirements = requirements;
this.earnings = earnings;
this.Id = Id;
this.nextCardId = nextCardId;
this.color = color;
this.photoName = photoName;
this.ageNumber = ageNumber;
this.orSituation = orSituation;
this.specialFunctionName = specialFunctionName;
this.used = false;
}
public Card(Card c){
this.name = c.name;
this.requirements = c.requirements;
this.earnings = c.earnings;
this.Id = c.Id;
this.nextCardId = c.nextCardId;
this.color = c.color;
this.photoName = c.photoName;
this.ageNumber = c.ageNumber;
this.orSituation = c.orSituation;
this.specialFunctionName = c.specialFunctionName;
this.used = c.used;
}
public Card() {
this.name = "";
this.requirements = new ArrayList<Material>();
this.earnings = new ArrayList<Material>();
this.Id = -1;
this.nextCardId = -1;
this.color = "";
this.photoName = "";
this.ageNumber = -1;
this.orSituation = false;
this.specialFunctionName = "";
this.used = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Material> getRequirements() {
return requirements;
}
public void setRequirements(ArrayList<Material> requirements){
this.requirements = requirements;
}
public ArrayList<Material> getEarnings() {
return earnings;
}
public void setEarnings(ArrayList<Material> earnings) {
this.earnings = earnings;
}
public int getId(){
return Id;
}
public void setId(int Id){
this.Id = Id;
}
public int getNextCardId(){
return nextCardId;
}
public void setNextCardId(int nextCardId){
this.nextCardId = nextCardId;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPhotoName() {
return photoName;
}
public void setPhotoName(String photoName) {
this.photoName = photoName;
}
public int getAgeNumber() {
return ageNumber;
}
public void setAgeNumber(int ageNumber) {
this.ageNumber = ageNumber;
}
public void setSpecialFunctionName(String specialFunctionName){
this.specialFunctionName = specialFunctionName;
}
public String getSpecialFunctionName(){
return this.specialFunctionName;
}
public boolean getOrSituation(){
return this.orSituation;
}
public void setOrSituation(boolean orSituation){
this.orSituation = orSituation;
}
public int getCardWarPoint() {
return cardWarPoint;
}
public int getNumberOfRequiredWood(){
for(int i = 0; i < requirements.size(); i++){
if(requirements.get(i).getName() == "Wood"){
return requirements.get(i).getCount();
}
}
return 0;
}
public int getNumberOfRequiredClay(){
for(int i = 0; i < requirements.size(); i++){
if(requirements.get(i).getName() == "Clay"){
return requirements.get(i).getCount();
}
}
return 0;
}
public int getNumberOfRequiredOre(){
for(int i = 0; i < requirements.size(); i++){
if(requirements.get(i).getName() == "Ore"){
return requirements.get(i).getCount();
}
}
return 0;
}
public int getNumberOfRequiredStone(){
for(int i = 0; i < requirements.size(); i++){
if(requirements.get(i).getName() == "Stone"){
return requirements.get(i).getCount();
}
}
return 0;
}
public int getNumberOfRequiredGlass(){
for(int i = 0; i < requirements.size(); i++){
if(requirements.get(i).getName() == "Glass"){
return requirements.get(i).getCount();
}
}
return 0;
}
public int getNumberOfRequiredLoom(){
for(int i = 0; i < requirements.size(); i++){
if(requirements.get(i).getName() == "Loom"){
return requirements.get(i).getCount();
}
}
return 0;
}
public int getNumberOfRequiredPapyrus(){
for(int i = 0; i < requirements.size(); i++){
if(requirements.get(i).getName() == "Papyrus"){
return requirements.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningWood(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Wood"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningStone(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Stone"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningOre(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Ore"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningClay(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Clay"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningGlass(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Glass"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningLoom(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Loom"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningPapyrus(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Papyrus"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningCivilian(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Civilian"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningMilitary(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "Military"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningScienceRuler(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "ScienceRuler"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningScienceWheel(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "ScienceWheel"){
return earnings.get(i).getCount();
}
}
return 0;
}
public int getNumberOfEarningScienceStone(){
for(int i = 0; i < earnings.size(); i++){
if(earnings.get(i).getName() == "ScienceStone"){
return earnings.get(i).getCount();
}
}
return 0;
}
public void setCardWarPoint(int cardWarPoint) {
this.cardWarPoint = cardWarPoint;
}
public void print(){
System.out.println("NAME: " + name + " ID:" + Id + " nextCardId: " + nextCardId + " color:" + color + " photoName: " + photoName + " ageNumber: "+ ageNumber + " orSituation: " + orSituation + " specialFunctionName: " + specialFunctionName);
System.out.print("Requirements: ");
for(int i = 0; i < requirements.size(); i++){
requirements.get(i).print();
}
System.out.println();
System.out.print("Earnings: ");
for(int i = 0; i < earnings.size(); i++){
earnings.get(i).print();
}
System.out.println();
}
@Override
public String toString() {
return "Card{" +
"name='" + name + '\'' +
", requirements=" + requirements +
", earnings=" + earnings +
", Id=" + Id +
", nextCardId=" + nextCardId +
", color='" + color + '\'' +
", photoName='" + photoName + '\'' +
", ageNumber=" + ageNumber +
", orSituation=" + orSituation +
", specialFunctionName='" + specialFunctionName + '\'' +
", cardWarPoint=" + cardWarPoint +
'}';
}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
}
这是处理纸牌和玩家的引擎
public class PlayerEngine extends Application {
private Player player;
private ArrayList<Bot> bots;
private ArrayList<Player> allPlayers;
public PlayerEngine(int numberOfPlayers, City desiredCityOfHumanPlayer, ArrayList<City> citiesOfBots){
allPlayers = new ArrayList<>();
bots = new ArrayList<>();
player = new Player(0, desiredCityOfHumanPlayer);
allPlayers.add(player);
for(int i = 0; i < numberOfPlayers - 1; i++){
Bot bot = new Bot(i, citiesOfBots.get(i));
allPlayers.add(bot);
bots.add(bot);
}
for(int i = 0; i < bots.size(); i++){
bots.get(i).print();
}
}
public PlayerEngine(int numberOfPlayers){
allPlayers = new ArrayList<>();
bots = new ArrayList<>();
player = new Player();
allPlayers.add(player);
for(int i = 0; i < numberOfPlayers - 1; i++){
Bot bot = new Bot(i);
allPlayers.add(bot);
bots.add(bot);
}
}
public Player getHumanPlayer(){
return player;
}
public ArrayList<Bot> getBots(){
return bots;
}
public void matchPlayerWithCity(Player player, City city){
player.setCity(city);
}
@Override
public void start(Stage primaryStage) throws Exception {
System.out.println("inside start");
CityManager cityManager = new CityManager();
cityManager.createACities();
cityManager.printACities();
cityManager.createBCities();
cityManager.printBCities();
Player p = new Player();
}
public void distributeHands(ArrayList<Card> cards){
System.out.println("number of players" + allPlayers.size());
System.out.println("number of cards" + cards.size());
int count = 0;
for(int i = 0; i < 7; i++){
player.addToHandAtFirst(cards.get(count));
count++;
}
for(int i = 0; i < bots.size(); i++){
for(int j = 0; j < 7; j++){
bots.get(i).addToHandAtFirst(cards.get(count));
count++;
}
}
}
public int[] numberOfCardsOfAllPlayers(){
int [] arr = new int[allPlayers.size()];
for(int i = 0; i < arr.length; i++){
arr[i] = allPlayers.get(i).getCards().size();
}
return arr;
}
public int getHighScore(){
int max=0;
for(int i=0;i < allPlayers.size();i++){
if( allPlayers.get(i).getScore()>max){
max = allPlayers.get(i).getScore();
}
}
return max;
}
public int getPlayerPoint(Player player) {
return player.getScore();
}
public void updatePlayerPoint(Player player,int point){
player.setScore(player.getScore()+point);
}
public void increaseCoin(Player player,int coinNumber){
player.setCoin(player.getCoin()+ coinNumber);
}
public void increaseWarPoints(Player player, int warPoints){
player.setWarPoints(player.getWarPoints() + warPoints);
}
public Player getPlayer(int index){
return allPlayers.get(index);
}
public ArrayList<Card> getCardsEntity(Player player){
return player.getCards();
}
public void addToCardList(Player player,Card card)
{
player.addCardsToTable(card);
}
public void disjointCard(Player player,int cardNum){
player.getCards().remove(player.getCards().get(cardNum));
}
public void startAttack(){
// attack manager ile bağlanacak.
}
public void finishAttack(){
// attack manager ile bağlanacak.
}
public void startXOX(Boolean bool){
//xox game ile bağlanacak.
}
public ArrayList<Player> getAllPlayers(){
return allPlayers;
}
public void printPlayers(){
System.out.println();
System.out.println("LIST OF ALL PLAYERS");
System.out.println();
System.out.println("PLAYER");
player.print();
System.out.println("BOTS");
for(int i = 0; i < bots.size(); i++){
bots.get(i).print();
}
}
}
答案 0 :(得分:0)
在您的http://localhost:62025/Forum/HomePage/GetReviews/1/Redmi-Note-5
您要向玩家和机器人添加相同的实例 他们通过相同的引用处理相同的对象 如果进行了修改,则会在相同的对象上进行修改,并且其他任何具有相同引用的对象都将可见