版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
版权
适配器类实现其中一个接口方法,创建另一个接口的对象和构造方法,在接口方法中调用另一个接口的方法。实例如下:
publicclassshipeiqi{publicstaticvoidmain(String[] args){ ATable aTable=newATable(){@Overridepublic StringgetCurrentUserName(){return"姓名";}@Overridepublic StringgetTableName(){return"表格";}}; BTable table=newTableAdapter(aTable); System.out.println(table.getHeaderText());}publicstaticclassTableAdapterimplementsBTable{private ATable aTable;publicTableAdapter(ATable aTable){this.aTable= aTable;}@Overridepublic StringgetHeaderText(){return"["+aTable.getCurrentUserName()+"]"+" : "+aTable.getTableName();}}publicinterfaceATable{ StringgetCurrentUserName(); StringgetTableName();}publicinterfaceBTable{ StringgetHeaderText();}}