简体   繁体   English

在Java 8中是否可以使用方法引用定义/实现方法?

[英]Is it possible in Java 8 to define/implement methods using method references?

Is there any way to define methods via method references, as demonstrated by the following imaginary code (and imaginary syntax)? 如以下虚构代码(和虚构语法)所示,是否有任何方法可以通过方法引用定义方法?

class A
{
    public Sometype somefunction(Argtype arg)
    { ... return somevalue; ..}
}

class B
{
    public Sometype method = A::somefunction;
}

or 要么

class C
{
    public Sometype method(Argtype arg) = A::somefunction;
}

A similar feature would seem quite useful to me. 类似的功能对我来说似乎很有用。 Methods are known at compile time, so any definition in class B or C might simply (after a type check) take over a reference to an already known method in A . 方法在编译时是已知的,所以类BC任何定义都可以简单地(在类型检查之后)接管对A已知方法的引用。 The advantage: faster calls (one less required than even in a direct call from a new method) - where I am not sure whether Java compilers do not optimize anyway? 优点:调用速度更快(比从新方法中直接调用所需的调用次数更少)-我不确定Java编译器是否仍未进行优化?

Is there any way to achieve that already now in Java (if so, it has evaded my search successfully)? 现在有什么方法可以用Java实现(如果是这样,它已经成功地避开了我的搜索)? Or are there good reasons not to introduce that to Java? 还是有充分的理由不将其引入Java? Are there any workarounds which provide similar performance advantage? 是否有任何变通办法可提供类似的性能优势?

PS: I get the message that similar question titles have been downvoted and/or closed. PS:我得到的消息是类似的问题标题已被否决和/或关闭。 If this applies here, too, please help me to understand why. 如果这也适用于此,请帮助我理解原因。 Thanks in advance! 提前致谢!

This question can be split in 2 different ones. 这个问题可以分为两个不同的问题。

  1. How can I use class A methods from other classes 如何使用其他类中的A类方法
  2. How can I specify the return type of a method at runtime 如何在运行时指定方法的返回类型

Regarding the first one, take a look at Java inheritance, polymorphism, abstract classes and interfaces. 关于第一个,请看一下Java继承,多态性,抽象类和接口。

I would have defined class A as abstract: 我将A类定义为抽象的:

abstract class A{
  public Sometype somefunction(Argtype arg)
     { ... return somevalue; ..}
}

and B, C extends it: B,C扩展它:

class B extends A{
}

In this way B and C inherits all the methods implemented in A and you can perform something like this: 这样,B和C继承了A中实现的所有方法,您可以执行以下操作:

B b = new B();
b.somefunction(arg);

Moreover, referring to the second question if you don't know the return type of this method at compile time you can exploit java generics (if polymorphism isn't enough). 此外,如果您在编译时不知道此方法的返回类型,请参考第二个问题,您可以利用Java泛型(如果多态性还不够的话)。 In this way class A becomes: 这样,A类变为:

abstract class A{
    public T somefunction(Class<T> type)
    { ... return somevalue; ..}
}

and the previous example code becomes: 前面的示例代码变为:

B b = new B();
b.somefunction(String.class);

You could do something like this: 您可以执行以下操作:

class A {
    public static Sometype someStaticFunction(Argtype aArg) {
      ...
    }

    public Sometype someNonStaticFunction(Argtype aArg) {
      ...
    }
}

class B {
    private Function<Argtype, Sometype> mStaticFunction = A::someStaticFunction;

    private Function<Argtype, Sometype> mNonStaticFunction = new A()::someNonStaticFunction;

    public Sometype applyStatic(Argtype aArg) {
      return mStaticFunction.apply(aArg);
    }

    public Sometype applyNonStatic(Argtype aArg) {
      return mNonStaticFunction.apply(aArg);
    }
}

However I don't see any point in this, regrading design, performance or any other aspects. 但是,我认为在这方面没有任何意义,无论是在改进设计,性能还是在其他方面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM