首页 文章

Lambda接口名称标准Java [关闭]

提问于
浏览
1

随着Java 8的发布,Lambda成了一件事 . 我已经熟悉JavaScript的语法了 . 然而,真正让我烦恼的是为用户界面选择正确的名称,因为在Java中,您需要为函数可能具有的每个不同输入创建一个接口 . 一个例子:

public class Start {

    public static void main(String[] args) {
        new Start().run();

    }

    private void run() {
        Lambda test = () -> {
            System.out.println("Test");
        };
        LambdaNumbers number = (int a, int b) -> a + b;
        test.foo();
        System.out.println(number.foo(5,5));
    }

}

interface Lambda {
    void foo();
}
interface LambdaNumbers {
    int foo(int a, int b);
}

一些更复杂的功能可以有自己独特的名称 . 但是,您可以猜测返回void的简单函数应该具有一个可以再现的名称 . 我目前正在使用此系统作为接口的名称:

  • Function: 返回虚空

  • Var: 返回字符串

  • Val: 返回双倍

  • Rog: 返回布尔值

  • Dec: 返回Int

例:

interface Function {
    void foo();
}
interface Var {
    String foo();
}
interface Val {
    double foo();
}
interface DecIntInt {
    int foo(int a, int b);
}

这只是我命名接口的方式 . 所以我想知道他们是不是一个不成文的规则如何围绕Java程序员命名这些东西?

1 回答

  • 3

    没有必要发明自己的接口 . 使用java.util.function中的通用参数:

    • FunctionRunnable

    • VarSupplier<String>

    • Val →以下之一:

    • DoubleSupplier

    • Supplier<Double>

    • DecIntInt →以下之一:

    • IntBinaryOperator

    • BinaryOperator<Integer>

    • ToIntBiFunction<Integer, Integer>

    • BiFunction<Integer, Integer, Integer>

相关问题