2015-12-08 86 views
1

在我的情况,我有使用Cache_Categories()方法很多地方,但有些时候不需要重写After_Success()方法是不覆盖After_Success()方法可以调用的方法,无需重载接口参数空

接口:

public interface Web_Service 
    { 
     public void After_Success(); 
    } 

方法:

public static void Cache_Categories(Web_Service i) 
    { 
    i.After_Success(); 
    } 

致电: 通常

new App_Methods().Cache_Categories(this, false, new App_Methods.Web_Service() 
     { 
      @Override 
      public void After_Success() 
      { 

      } 
     }); 

通缉:

new App_Methods().Cache_Categories(this, false,null); 
+0

java-8有能力做到这一点。请遵循java-8特性的默认方法。 –

回答

2

只是检查,如果web服务为空,不调用它的方法。

0

将接口更改为抽象类。

public abstract class Web_Service 
{ 
    public abstract void after_Success(); 
} 
+0

和方法名称应以小写字母开头 –

0

我,你可以使用Java 8,那么你可以在这种情况下,改变

public static void Cache_Categories(SomeType type, Boolean b, Web_Service i) 
    { 
     i.After_Success(); 
    } 

public static void Cache_Categories(SomeType type, Boolean b, Optional<Web_Service> i) 
    { 
     i.ifPresent(Web_Service::After_Success); 
    } 

您只要致电:

new App_Methods().Cache_Categories(this, false, Optional.ofNullable(ws)); 

ws可能参数,局部var,obj等属性,可能为空。