Java 接口参数null

Java 接口参数null,java,Java,在我的场景中,我在很多地方使用了Cache\u Categories()Method,但有时不需要重写After\u Success()Method,可以在不重载的情况下调用Method而不重写After\u Success()Method 接口: public interface Web_Service { public void After_Success(); } 方法: public static void Cache_Categories(Web_S

在我的场景中,我在很多地方使用了
Cache\u Categories()
Method,但有时不需要重写
After\u Success()
Method,可以在不重载的情况下调用Method而不重写
After\u Success()
Method

接口:

 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,null);

只需检查webService是否为null,不要调用它的方法

将接口更改为抽象类

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

如果使用Java 8,则可以更改:

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


ws
可以是参数、局部变量、对象属性,并且可以为null时。

java-8可以做到这一点。请遵循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));