Java equivalent Delphi language features

See English answer > equivalent of c# anonymous methods in Java? 6

I see from the Java language specification that it supports lambda expressions, so I think I can find some examples showing that Java is equivalent to anonymous methods

My question is whether Java has the following equivalent Delphi types, and if so, their names or equivalent Java constructs:

>Traditional procedure, function type, such as myproc = procedure (I: integer) type > object and function of object

(I hope these are close enough to a problem)

Solution

>In Java, methods are never independent, they are always bound to interfaces / classes So nothing can create a type reference

It uses a lambda expression and assigns it to the variable allpilots

Predicate<Person> allPilots = p -> p.getAge() >= 23 && p.getAge() <= 65;

... and use it to call the method from different places:

System.out.println("\n=== Mail all Pilots ===");
robo.mailContacts(pl,allPilots);
...
public void mailContacts(List<Person> pl,Predicate<Person> pred) {
    for (Person p : pl) {
        if (pred.test(p)) {
            roboEmail(p);
        }
    }
}

The predicate interface is defined as

public interface Predicate<T> {
    public boolean test(T t);
}

This is close to the function of the object in Delphi (because the interface has a return type) For object methods, the function interface simply has no return type (void)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>