简体   繁体   English

Java 谓词接口 - 该方法如何工作 - 虽然它没有在我的 class 中实现

[英]Java Predicate Interface - How the method is working - Though it is not implemented in my class

Though I have not implemented the method - test - which is in Predicate interface, how come the method is executed without any body in my class - inte??虽然我还没有实现 Predicate 接口中的方法 - 测试,但为什么在我的 class - inte 中没有任何主体执行该方法?

package newone; 
import java.util.function.Predicate;

public class inte {
    public static void main(String[] args) {
        Predicate <Integer> p = i -> (i<19);
        System.out.println(  p.test(22));
       }


@FunctionalInterface
public interface Predicate<T> {
    boolean test(T var1);
}

Though I have not implemented the method - test虽然我还没有实现该方法 - 测试

You have, with the lambda expression:您拥有 lambda 表达式:

Predicate <Integer> p = i -> (i<19);

That's mostly like :主要是

Predicate<Integer> p = new Predicate<Integer>() {
    @Override
    public boolean test(Integer i) {
        return i < 19;
    }
};

The exact details in the bytecode are a little different, but for the most part you can think of a lambda expression as shorthand for creating an anonymous inner class.字节码中的具体细节略有不同,但在大多数情况下,您可以将 lambda 表达式视为创建匿名内部 class 的简写。 The compiler is able to perform other tricks to avoid creating extra classes in reality, at least in some cases - it depends on what your lambda expression does.编译器能够执行其他技巧以避免在现实中创建额外的类,至少在某些情况下 - 这取决于您的 lambda 表达式的作用。

Newer compiler 1 , starting with version 8 (I believe), add a (synthetic) method to the code, mostly like :较新的编译器1 ,从版本 8(我相信)开始,在代码中添加一个(合成)方法,主要是

public class Inte {
    public static void main(String[] args) {
        Predicate<Integer> p = Inte::lambda$test$1;
        ...
    }
    // synthetic
    private static boolean lambda$test$1(Integer i) {
        return i < 19;
    }
}

1 - alternatively, and before Java version 8, an additional class implementing the interface can be added (like an anonymous class) 1 - 或者,在 Java 版本 8 之前,可以添加一个额外的 class 实现接口(如匿名类)

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

相关问题 JAVA:access方法在类中定义,但未通过其接口实现 - JAVA :access method defined in class , but not implemented by its interface 如何找到在给定类中实现其方法的Java接口? - How do I find the Java interface whose method is implemented in a given class? Java:如何从接口名称调用实现的方法 - Java:How to call Implemented method from interface name Java如何使对象使用它实现的静态接口方法 - Java How to make object use it's implemented static interface method 如何限制java接口在多个类中实现 - how to restrict java interface to be implemented in more than one class 这个接口和class是怎么实现的? - How is this interface and class being implemented? 如何更改从实现到我正在使用的具体类的接口覆盖的方法的签名? - How do I change the signature of a method I have overriden from my interface that I implemented onto the concrete class I'm using? 如何将实现的接口对象传递给其实现的类中的方法? - How do I pass an implemented Interface object to a method in the class its implemented in? 声明接口方法以仅允许实现的类 - Declaring interface method to allow only implemented class 未在实现类中实现的EventListener接口中的handleEvent方法 - handleEvent method in EventListener interface not implemented in the implementing class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM