2015-10-26 32 views
0

調用同一個類中從另一個方法的方法在同一類兩種方法,第一種方法是getEventByEventId秒方法是addEventUserRealtionMapping現在我需要調用getEventByEventId方法爲addEventUserRelationMapping方法我怎麼能我做到了。我如何在Java

public EventBO getEventByEventId(long eventId) throws UserServiceException { 
     try { 
      EventDO eventDO = eventDAOImpl.getEventByEventId(eventId); 
      EventBO eventBO = mapper.mapEventDOToBO(eventDO, new EventBO()); 
      return eventBO; 
     } catch (UserDataException uExp) { 
      throw new UserServiceException("Error while getting event for event id " + eventId, uExp); 
     } 
    } 

public int addEventUserRealtionMapping(ArrayList<UserBO> userBOs, long eventId) throws UserServiceException { 

     List<EventUserRelationDO> totalAddedCount; 
     ArrayList<UserDO> userDOs = new ArrayList<>(); 
     try { 
      for (UserBO userBO : userBOs) { 
       UserDO userDO = mapper.mapUserBOToDO(userBO, new UserDO()); 
       userDOs.add(userDO); 
      } 
      EventBO eventBO =new EventBO(); 
      eventBO =getEventByEventId(eventId);//I am try that while call method1 but it doesn't work 
      MessageBO messageBO = new MessageBO(); 
      messageBO.setEventId(eventBO.getEventId()); 
      messageBO.setEventTitle(eventBO.getText()); 
      messageBO.setMessage(eventBO.getText()); 
      messageBO.setRingeeUserId(eventBO.getRingeeUserId()); 
      messageBO.setMessageType(IRingeeConstants.MESSAGE_TYPE_INVITATION); 
      totalAddedCount = eventDAOImpl.addEventUserRelationMapping(userDOs, eventId); 
      if (totalAddedCount.size() == userDOs.size()) { 
       manageMessageService.sendMessageToGroup(userBOs, messageBO); 
      } 
     } catch (UserDataException dExp) { 
      throw new UserServiceException(" exception while adding user relationship for eventId " + eventId, dExp); 
     } 
     return totalAddedCount.size(); 
    } 
+1

你不知道如何調用一個方法? – Stultuske

+0

我知道如何調用一個方法,但我在這裏我想調用並設置返回值eventBO ..我不知道該怎麼做。 –

+0

似乎是循環遞歸方法調用?我說明顯? –

回答

2

您可以通過編寫methodName()

class ClassName{ 

    public void method1(){ 
    } 

    public void method2(){ 

    this.method1(); // called the first method of the same class 
    } 
} 
1

您可以輕鬆地調用功能,但你需要把getEventByEventId嘗試捕捉內部或拋出UserServiceException形式addEventUserRealtionMapping調用它使用this.methodName()或直接

如果你有無限循環,它似乎是你的mapEventDOToBO調用addEventUserRealtionMapping裏面,你是嘗試getEventByEventId再次,使它導致無限循環你