In the previous example, the original role has only one role. In this case, we only need to inherit the role. If there are multiple implementation classes, Java does not support multiple inheritance, we will re-write an implementation class, and then reference other classes, for example:
Package adapter. demo; public interface iuserinfo {// obtain the user name Public String GetUserName (); // obtain the home address public string gethomeaddress (); // obtain the mobile phone number Public String gethomenumber (); // obtain the office phone number Public String getofficephonenum (); // obtain the company's position Public String getjobposition (); // obtain the home phone number Public String gethomephone ();}
Package Adapter. Demo; import java. util. Map; public interface iouteruserofficeinfo {// public map getuserofficeinfo ();}
Package Adapter. Demo; import java. util. Map; public interface iouteruserhomeinfo {// public map getuserhomeinfo ();}
Package Adapter. Demo; import java. util. Map; public interface iouteruserbaseinfo {// obtain the basic information of the user public map getuserbaseinfo ();}
package adapter.demo;import java.util.HashMap;import java.util.Map;public class OuterUserBaseInfo implements IOuterUserBaseInfo{@Overridepublic Map getUserBaseInfo() {HashMap baseInfoMap = new HashMap();baseInfoMap.put("username1 ", "name");baseInfoMap.put("mobile1", "1234455");return baseInfoMap;}}
package adapter.demo;import java.util.HashMap;import java.util.Map;public class OuterUserHomeInfo implements IOuterUserHomeInfo{@Overridepublic Map getUserHomeInfo() {HashMap baseInfoMap = new HashMap();baseInfoMap.put("username ", "name");baseInfoMap.put("mobile", "1234455");return baseInfoMap;}}
package adapter.demo;import java.util.HashMap;import java.util.Map;public class OuterUserOfficeInfo implements IOuterUserOfficeInfo{@Overridepublic Map getUserOfficeInfo() {HashMap baseInfoMap = new HashMap();baseInfoMap.put("username11 ", "name");baseInfoMap.put("mobile11", "1234455");return baseInfoMap;}}
Here is the focus, to see how to use the adapter for multi-Inheritance
Package adapter. demo; import Java. util. map; public class outeruserinfo implements iuserinfo {// source object target private iouteruserbaseinfo baseinfo = NULL; private iouteruserofficeinfo officeinfo = NULL; private iouteruserhomeinfo homeinfo = NULL; // data processing private map basemap = NULL; private map homemap = NULL; private map officemap = NULL; // The constructor transmits public outeruserinfo (iouteruserbaseinfo _ baseinfo, iouteruserofficeinfo _ officeinfo, iouteruserhomeinfo _ homeinfo) {This. baseinfo = _ baseinfo; this. officeinfo = _ officeinfo; this. homeinfo = _ homeinfo; this. basemap = This. baseinfo. getuserbaseinfo (); this. homemap = This. homeinfo. getuserhomeinfo (); this. officemap = This. officeinfo. getuserofficeinfo () ;}@ overridepublic string GetUserName () {string hometelnum = (string) This. homemap. get ("username"); system. out. println (hometelnum); Return hometelnum;} @ overridepublic string gethomeaddress () {string homeaddress = (string) This. homemap. get ("mobile"); system. out. println (homeaddress); Return homeaddress;} @ overridepublic string get1_number () {string 1_num = (string) This. homemap. get ("mobile"); Return writable num;} @ overridepublic string getofficephonenum () {// todo auto-generated method stubreturn NULL;} @ overridepublic string getjobposition () {return NULL ;} @ overridepublic string gethomephone () {// todo auto-generated method stubreturn NULL ;}}
package adapter.demo;public class Client {public static void main(String[] args) {IOuterUserHomeInfo homeinfo = new OuterUserHomeInfo();IOuterUserBaseInfo baseinfo = new OuterUserBaseInfo();IOuterUserOfficeInfo office = new OuterUserOfficeInfo();IUserInfo girl = new OuterUserInfo(baseinfo, office, homeinfo);girl.getHomeAddress();}}