如何从java中同一个类中的另一个方法调用方法

时间:2015-10-26 07:39:26

标签: java

我在同一个类中有两个方法,第一个方法是getEventByEventId第二个方法是addEventUserRealtionMapping现在我需要调用getEventByEventId方法到addEventUserRelationMapping方法我怎么能这样做。

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();
    }

2 个答案:

答案 0 :(得分:2)

您可以使用this.methodName()或直接通过撰写methodName()

来调用它
class ClassName{

  public void method1(){
  }

  public void method2(){

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

答案 1 :(得分:1)

您可以轻松调用函数,但是您需要将getEventByEventId放在try catch中或抛出UserServiceException形式addEventUserRealtionMapping

如果你有无限循环,似乎mapEventDOToBO在其中调用addEventUserRealtionMapping,你再次尝试getEventByEventId,这样就会导致无限循环你