问题描述
publicclassMyGame{privatestaticMyGamemyGame;privateArrayList<Game>participateGame;privateArrayList<Game>organizeGame;privateMyGame(ArrayList<Game>orgGameList,ArrayList<Game>parGameList){System.out.println("MyGamecontruct");organizeGame=orgGameList;participateGame=parGameList;}publicstaticMyGamegetMyGame(){if(myGame==null){System.out.println("myGame==null");ArrayList<Game>org=newArrayList<Game>();ArrayList<Game>par=newArrayList<Game>();myGame=newMyGame(org,par);}System.out.println("retrunmyGame");returnmyGame;}publicArrayList<Game>getParticipateGame(){returnparticipateGame;}publicvoidsetParticipateGame(ArrayList<Game>participateGame){this.participateGame=participateGame;}publicArrayList<Game>getOrganizeGame(){System.out.println("getOrganizedGame");if(organizeGame==null){System.out.println("organizeGame==null");}returnorganizeGame;}publicvoidsetOrganizeGame(ArrayList<Game>organizeGame){this.organizeGame=organizeGame;}}为啥我通过方法getOrganziedGame()为空?
解决方案
解决方案二:
importjava.util.*;classGame{}publicclassMyGame{privatestaticMyGamemyGame;privateArrayList<Game>participateGame;privateArrayList<Game>organizeGame;privateMyGame(ArrayList<Game>orgGameList,ArrayList<Game>parGameList){System.out.println("MyGamecontruct");organizeGame=orgGameList;participateGame=parGameList;}publicstaticMyGamegetMyGame(){if(myGame==null){System.out.println("myGame==null");ArrayList<Game>org=newArrayList<Game>();ArrayList<Game>par=newArrayList<Game>();myGame=newMyGame(org,par);}System.out.println("retrunmyGame");returnmyGame;}publicArrayList<Game>getParticipateGame(){returnparticipateGame;}publicvoidsetParticipateGame(ArrayList<Game>participateGame){this.participateGame=participateGame;}publicArrayList<Game>getOrganizeGame(){System.out.println("getOrganizedGame");if(organizeGame==null){System.out.println("organizeGame==null");}returnorganizeGame;}publicvoidsetOrganizeGame(ArrayList<Game>organizeGame){this.organizeGame=organizeGame;}publicstaticvoidmain(String[]args){System.out.println("--------"+MyGame.getMyGame().getOrganizeGame());}}
输出为myGame==nullMyGamecontructretrunmyGamegetOrganizedGame--------[]//可以看到organizedGame不为null
解决方案三:
你是直接调的getOrganziedGame这个方法吧,你这代码是在getgetMyGame里面赋值的;2楼的调用放大才是正确的。