Java作为对象运行
在Java中,函数作为对象有很好的用途,也就是说,有以下几类:Java作为对象运行,java,function,jvm,Java,Function,Jvm,在Java中,函数作为对象有很好的用途,也就是说,有以下几类: Function handle_packet_01 = void handle() {} 我不想使用Scala,因为我受不了它的语法 是否有任何类型的黑客可以应用到JVM来允许我这样做?Eclipse插件怎么样 我在Java中看到了类似的操作符重载,我也将为其安装插件。在Java 8中,您可以引用如下成员方法 MyClass::function 编辑:更完整的示例 //For this example I am creating
Function handle_packet_01 = void handle() {}
我不想使用Scala,因为我受不了它的语法
是否有任何类型的黑客可以应用到JVM来允许我这样做?Eclipse插件怎么样
我在Java中看到了类似的操作符重载,我也将为其安装插件。在Java 8中,您可以引用如下成员方法
MyClass::function
编辑:更完整的示例
//For this example I am creating an interface that will serve as predicate on my method
public interface IFilter
{
int[] apply(int[] data);
}
//Methods that follow the same rule for return type and parameter type from IFilter may be referenced as IFilter
public class FilterCollection
{
public static int[] median(int[]) {...}
public int[] mean(int[]) {...}
public void test() {...}
}
//The class that we are working on and has the method that uses an IFilter-like method as reference
public class Sample
{
public static void main(String[] args)
{
FilterCollection f = new FilterCollection();
int[] data = new int[]{1, 2, 3, 4, 5, 6, 7};
//Static method reference or object method reference
data = filterByMethod(data, FilterCollection::median);
data = filterByMethod(data, f::mean);
//This one won't work as IFilter type
//data = filterByMethod(data, f::test);
}
public static int[] filterByMethod(int[] data, IFilter filter)
{
return filter.apply(data);
}
}
另请看另一个示例和方法引用的用法请阅读java 8 api。此外,您可以通过使用handle方法的语法创建接口来模拟这种方法。谢谢,我现在就去阅读。因为这只是一个服务器,Java 8很好。@GergelyBacso,有一个Eclipse插件,可以让你在Java代码中使用运算符重载,所以一切都有可能,对吧?多亏了那些建议lambdas的人,他们非常适合这个。快速提问,过滤器的类型是什么::中值?@Lolums我现在提供了一个更好、更完整的示例。