jeudi 19 janvier 2017

Add a Method to Object Array

I would like to fill a Method in an Array. But I have to do this in a strange way:

public class AssignFunctionalInterfaceTest {

    public void test() {
        TestEntity t = new TestEntity();

        // compiles
        Supplier<Integer> a = t::getB;
        Object[] b = new Object[] {"42", a};

        // compiles
        Object[] b2 = new Object[] {"42", (Supplier<Integer>) t::getB};

        // doesn't compile
        Object[] b3 = new Object[] {"42", t::getB};
    }


    public static class TestEntity {

        public Integer getB() {
            return 0;
        }
    }
}

I would really like to find a way to make the third assignment work. Any Ideas?





Aucun commentaire:

Enregistrer un commentaire