简体   繁体   English

java8 中“使用 Classname.staticMethod 调用 static 方法”和“使用 Classname::staticMethod 调用”之间有什么区别吗

[英]Is there any difference between “invoking a static method with Classname.staticMethod” & “invoking with Classname::staticMethod” in java8

Is there any difference between “invoking a static method with Classname.staticMethod” and “invoking a static method with Classname::staticMethod” in java?在 Z93F725A07423FE1C8846F448B33D21 中“使用 Classname.staticMethod 调用 static 方法”和“使用 Classname::staticMethod 调用 static 方法”之间有什么区别吗?

Also is there a difference between “invoking a method with Object.method” and “invoking a method with Object::method” in java?在 Z93F725A07423FE1C8846F448B33D21

Classname::staticMethod or Object::method syntax is for Consumer operations are Consumer operation representations where compiler ensures the method is accepts the given type as input type While class.method or object.method are regular method invocations:) Classname::staticMethod 或 Object::method 语法用于消费者操作是消费者操作表示,其中编译器确保方法接受给定类型作为输入类型而 class.method 或 ZA8CFDE6331BD59EB2AC96F8911C4B6 是常规方法调用:)

The use of:: denotes a Method Reference and these can be used in any place where a @FunctionalInterface is required.使用 of:: 表示方法引用,这些可以在任何需要@FunctionalInterface的地方使用。 Examples are Consumer s, Function s, Predicate s, Supplier s etc.... A lot of the Java 8 features.示例是Consumer s、 Function s、 Predicate s、 Supplier s 等...... Java 8 的许多功能。 Eg if a method requires a String which is provided through a Supplier<String>例如,如果一个方法需要一个通过Supplier<String>提供的字符串

public int methodUsingSupplier(Supplier<String> supplier) {
    String s = supplier.get();
    ...
}

and there is a method并且有一个方法

public static String strProvider() {
    return "my String";
}

in "MyClass"在“我的班级”中

then you can:那么你也能:

int value = methodUsingSupplier(MyClass::strProvider);

as a way of passing the strProvider method as a 'reference' to the methodUsingSupplier method which can then utilise it.作为将strProvider方法作为“引用”传递给methodUsingSupplier方法的一种方式,该方法可以使用它。

As previously said in other answers the compiler ensures that the types and arguments match正如前面在其他答案中所说,编译器确保类型和 arguments 匹配

You can use both approaches to invoke a method, and the method will behave in the same way.您可以使用这两种方法来调用一个方法,并且该方法将以相同的方式运行。 However you need different syntaxes for both cases.但是,这两种情况都需要不同的语法。 In the first approach you "directly" call the method to run its code.在第一种方法中,您“直接”调用该方法来运行其代码。 In the second approach you use a reference to the method and apply the methods arguments (if any) to a method defined on the reference to actually run the code.在第二种方法中,您使用对方法的引用并将方法 arguments(如果有)应用于在引用上定义的方法以实际运行代码。 This works both for static and instance methods.这适用于 static 和实例方法。

public static void staticMethodExample() {
    double d = 1.23;
    double d2 = 4.56;

    int x = Double.compare(d, d2);           // -1

    Comparator<Double> c = Double::compare;
    int y = c.compare(d, d2);                // -1
}


public static void instanceMethodExample() {
    Double d = 1.23;
    Double d2 = 4.56;

    int x = d.compareTo(d2);               // -1

    DoubleToIntFunction c = d::compareTo;
    int y = c.applyAsInt(d2);              // -1
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中“用类名调用静态方法”和“用对象调用静态方法”之间有什么区别吗? - is there any difference between “invoking a static method with class name” and “invoking a static method with an object” in java? 为什么eclipse告诉我应该为静态方法引用ClassName :: staticMethod“以静态方式访问”? - Why does eclipse tell me I should “accessed in a static way” for static method reference ClassName::staticMethod? JAVA:使用className和实例的Object进行静态方法调用之间的区别 - JAVA: difference between Static method invocation with className and with Object of instance ClassName.m() 和 (new ClassName()).m() m() 有什么区别是 static 方法 - What is the difference between ClassName.m() and (new ClassName()).m() m() is a static method Java - 类“ClassName”和公共类“ClassName”之间的区别 - Java - The difference between class “ClassName” and public class “ClassName” 在构造函数中调用虚拟方法:Java和C ++之间的区别 - Invoking virtual method in constructor: difference between Java and C++ 之间的区别 <className> .class和this(代表 <classname> ) - Difference between <className>.class and this(representing the <classname>) 使用Java反射库调用静态方法 - Invoking a static method with java reflection library Java 中 className.class.someMethod 与 className.this.someMethod 的区别? - Difference between className.class.someMethod vs className.this.someMethod in Java? Java中的调用方法问题 - Invoking method issue in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM