Java Lambda表达式参数 [英] Java Lambda expression parameters

查看:145
本文介绍了Java Lambda表达式参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的Lambda表达式来理解以下方法参考代码:

I'm trying the understand the following method reference code using Lambda expressions in Java:

interface MyFunc<T> {

    boolean func(T v1, T v2);
}

class HighTemp {

    private int hTemp;

    HighTemp(int ht) {
        hTemp = ht;
    }

    boolean sameTemp(HighTemp ht2) {
        return hTemp == ht2.hTemp;
    }

    boolean lessThanTemp(HighTemp ht2) {
        return hTemp < ht2.hTemp;
    }
}

class InstanceMethWithObjectRefDemo {

    static <T> int counter(T[] vals, MyFunc<T> f, T v) {
        int count = 0;
        for (int i = 0; i < vals.length; i++) {
            if (f.func(vals[i], v)) {
                count++;
            }
        }
        return count;
    }

    public static void main(String args[]) {
        int count;

        HighTemp[] weekDayHighs = {new HighTemp(89), new HighTemp(82),
            new HighTemp(90), new HighTemp(89),
            new HighTemp(89), new HighTemp(91),
            new HighTemp(84), new HighTemp(83)};

        count = counter(weekDayHighs, HighTemp::sameTemp,
                new HighTemp(89));
        System.out.println(count + " days had a high of 89");

        HighTemp[] weekDayHighs2 = {new HighTemp(32), new HighTemp(12),
            new HighTemp(24), new HighTemp(19),
            new HighTemp(18), new HighTemp(12),
            new HighTemp(-1), new HighTemp(13)};

        count = counter(weekDayHighs2, HighTemp::sameTemp,
                new HighTemp(12));
        System.out.println(count + " days had a high of 12");

        count = counter(weekDayHighs, HighTemp::lessThanTemp,
                new HighTemp(89));
        System.out.println(count + " days had a high less than 89");

        count = counter(weekDayHighs2, HighTemp::lessThanTemp,
                new HighTemp(19));
        System.out.println(count + " days had a high of less than 19");
    }
}

我的问题是,为什么函数sameTemp只有一个参数?接口声明它必须有2个参数,所以对我来说没有意义。也许hTemp实例变量对两者都足够了?我从Java那里拿了这本完整的参考书,作者在那里解释了这个:

My question is, why function sameTemp has just one parameter? The interface declare sthat it must have 2 parameters, so make no sense for me. Maybe the hTemp instance variable is enough for both? I took this from Java the complete reference book and there the author explains this:


Blockquote

Blockquote

在程序中,请注意HighTemp有两个实例方法: sameTemp()
lessThanTemp()。如果两个HighTemp对象包含相同的
温度,则第一个返回true。如果调用对象的温度小于传递对象的温度
,则第二个返回true。每个方法都有一个HighTemp类型的参数,每个方法都有一个布尔结果。因此,每个都与MyFunc功能接口
兼容,因为调用对象类型可以映射到 func()的第一个参数和
参数映射到 func()的第二个参数。

In the program, notice that HighTemp has two instance methods: sameTemp( ) and lessThanTemp( ). The first returns true if two HighTemp objects contain the same temperature. The second returns true if the temperature of the invoking object is less than that of the passed object. Each method has a parameter of type HighTemp and each methoreturns a boolean result. Thus, each is compatible with the MyFunc functional interface because the invoking object type can be mapped to the first parameter of func( ) and the argument mapped to func( )’s second parameter.

谢谢!

推荐答案

许多人在进行面向对象编程时忽略了......这是如何真正实现的。假设:

What many people overlook when doing OO programming is ... how that is really implemented. Assume:

class C {
  public int foo(int parm) { return parm; }

重点是:在对象上调用方法的想法是抽象。你将如何在汇编程序中实现它;这样CPU就可以实际执行该代码。你意识到:呼唤是不可能的。在那个级别,你只在内存中的某个地方调用函数。但是一个函数如何知道它自己的对象?!

The point is: the idea of calling a method "on" an object is an abstraction. How would you implement that in assembler; so that a CPU can actually execute that code. You realize: "calling on" isnt possible there. On that level, you only call functions somewhere in memory. But how would a function know about its owning object?!

导致很少有人知道的事情 - 您可以将上述代码编写为:

Leading to something that few people know - you can write the above code as:

class C {
  public int foo(C this, int i) {
        System.out.println("i: " + i);
        return i;
   }
}

为了安全起见,我还写了一篇文章日食中的小测试:

To be on the safe side, I also wrote a little test in eclipse for that:

@Test
public void testIt() {
    assertThat(new C().foo(5), is(5));
}

我的日食,单位测试通行证运行良好,并打印i:5 。

Runs fine in my eclipse, unit test passes, and prints "i: 5".

所以,整个秘诀是:在对象上定义方法时,总会有一个(通常是不可见的)第一个参数this 考虑到这种方法!

So, the whole secret is: when defining methods on an object, there is always an (normally invisible) first argument "this" given to that method!

因此:虽然你的方法看起来只有一个参数,但是missing参数是你调用方法的对象!

Thus: although your methods look like taking only one parameter, the "missing" parameter is the object on which you are calling the method!

在这个意义上:布尔函数(T v1,T v2)要求输入类型为T的两个参数这两种方法,如 boolean sameTemp(HighTemp ht2) ...可以写成 boolean sameTemp(HighTemp this,HighTemp ht2); et voila:T型的两个参数。

And in that sense: boolean func(T v1, T v2) asks for two parameters of type T. And both methods, like boolean sameTemp(HighTemp ht2) ... can be written as boolean sameTemp(HighTemp this, HighTemp ht2); et voila: two parameters of type T.

这篇关于Java Lambda表达式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆