java - 接口(interface)方法与类方法

标签 java class interface java-8

在 Java-8 中,我们可以有一个方法,例如,以多种方式屏蔽字符串:

接口(interface)实现

public interface StringUtil {
    static String maskString(String strText, int start, int end, char maskChar)
            throws Exception{

        if( Strings.isNullOrEmpty(strText))
            return "";

        if(start < 0)
            start = 0;

        if( end > strText.length() )
            end = strText.length();

        if(start > end)
            throw new Exception("End index cannot be greater than start index");

        int maskLength = end - start;

        if(maskLength == 0)
            return strText;

        StringBuilder sbMaskString = new StringBuilder(maskLength);

        for(int i = 0; i < maskLength; i++){
            sbMaskString.append(maskChar);
        }

        return strText.substring(0, start)
                + sbMaskString.toString()
                + strText.substring(start + maskLength);
    }
}

可以通过以下方式访问:

StringUtil.maskString("52418100", 2, 4, 'x')

现在可以通过类实现,如下所示

public class StringUtil {

    public String maskString(String strText, int start, int end, char maskChar)
                throws Exception{

            if( Strings.isNullOrEmpty(strText))
                return "";

            if(start < 0)
                start = 0;

            if( end > strText.length() )
                end = strText.length();

            if(start > end)
                throw new Exception("End index cannot be greater than start index");

            int maskLength = end - start;

            if(maskLength == 0)
                return strText;

            StringBuilder sbMaskString = new StringBuilder(maskLength);

            for(int i = 0; i < maskLength; i++){
                sbMaskString.append(maskChar);
            }

            return strText.substring(0, start)
                    + sbMaskString.toString()
                    + strText.substring(start + maskLength);
    }
}

可以通过以下方式访问:

StringUtil su = new StringUtil()
String mask = su.maskString("52418100", 2, 4, 'x')

问题:

在哪种情况下必须首选哪个?到目前为止,我知道 interface 函数无法通过 mock 进行测试,因此我必须在接口(interface)函数之上有一个包装函数 - 简而言之 em> - 它不适合单元测试,而且,由于是静态,您无法重写接口(interface)方法。

那么,如果可以选择编写函数,您还会考虑哪些其他用例?

最佳答案

对于任何明确开发的实用程序,我建议使用类。 java 中的默认方法有特殊用途。这些旨在成为需要供应商提供实现的实用方法(还记得吗? - 接口(interface)是供应商和用户之间的契约(Contract))。

例如,如果您正在使用任何第三方库,并且有一天供应商引入了新的实用程序功能,那么要么所有客户都必须在界面内覆盖该方法,要么只是供应商添加静态默认方法。这样,新库的代码仍然向后兼容(在二进制代码级别)。

准确地说,函数式接口(interface)的用法和目的在 Oracle 文档中得到了很好的解释,如下所示:-

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.

更多内容可以阅读官方文档here .

此外,考虑到这一目的,在您编写默认方法之前,永远不需要测试默认方法。如果实用方法很复杂,则应该对其进行测试。这些实用程序肯定可以进行集成测试。另外,正如您所说,您可以编写一个简单的接口(interface)实现并测试这些东西。这类似于测试抽象类。阅读 here了解更多详情。

关于java - 接口(interface)方法与类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49642316/

相关文章:

java - 类构造函数对象无法正确显示

Java 反射 : Create an implementing class

java - Eclipse 中的 JUnit 测试计时

java - Java对象的实例大小在不同的JVM中是不同的

c++ - '&' 在类的一元运算符中意味着什么?

java - 添加两个对象——难以理解接受一个对象作为参数

java - 在正文异常 spring rest 中添加新字段

java - jtable 内的动态 jcombobox 渲染

c# - 接口(interface)实现中的公共(public)元素

c# - 比较 : interface methods vs virtual methods vs abstract methods