In the development process, it is often the case that a function needs to return multiple values, this is a problem!!
The solution to this problem online:
1. Use map return value; The problem with this method is that you don't know how to return the value of the key, which can only be viewed through doc or through source code.
2. Pass in a reference and modify the referenced property value. Problem: not practical.
3, through the generic structure of a python-like tuple class, or construct a javabean, the problem is "one-time", not graceful.
Personal solutions:
Using Enummap as the return value type, define an enum yourself and define the property names that might be returned as enum values.
Package Com.cy;import java.util.enummap;/** * Created-on 2014/7/10. */public interface Testservice { enum userinfoproperty { room,cellphone,name } public enummap< Userinfoproperty,object> getuserinfobyname (String name);}
This class is implemented
public class Testserviceimpl implements Testservice { @Override public enummap<userinfoproperty, Object > getuserinfobyname (String name) { enummap<userinfoproperty,object> retmap = new enummap< Userinfoproperty, object> (userinfoproperty.class); Retmap.put (Userinfoproperty.room, "0003"); Retmap.put (Userinfoproperty.cellphone, "00004"); Retmap.put (userinfoproperty.name,name); return retmap;} }
This class is the main entrance
public class App {public static void Main (string[] args) { Testservice testservice = new Testserviceimpl (); String name = "TestName"; enummap<testservice.userinfoproperty,object> userInfo = testservice.getuserinfobyname (name); Userinfo.entryset (). iterator (); System.out.println (UserInfo.get (TestService.UserInfoProperty.Name)); System.out.println (UserInfo.get (TestService.UserInfoProperty.ROOM)); System.out.println (UserInfo.get (TestService.UserInfoProperty.CELLPHONE));} }
Java a function Enummap returns multiple values