我正在玩Java 8,以了解如何作为一等公民的功能.我有以下代码段:
package test;
import java.util.*;
import java.util.function.*;
public class Test {
public static void myForEach(List<Integer> list, Function<Integer, Void> myFunction) {
list.forEach(functionToBlock(myFunction));
}
public static void displayInt(Integer i) {
System.out.println(i);
}
public static void main(String[] args) {
List<Integer> theList = new ArrayList<>();
theList.add(1);
theList.add(2);
theList.add(3);
theList.add(4);
theList.add(5);
theList.add(6);
myForEach(theList, Test::displayInt);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是使用方法引用displayInt将方法传递给方法myForEach.编译器会产生以下错误:
src/test/Test.java:9: error: cannot find symbol
list.forEach(functionToBlock(myFunction));
^
symbol: method functionToBlock(Function<Integer,Void>)
location: class Test
src/test/Test.java:25: error: method myForEach in class Test cannot be applied …Run Code Online (Sandbox Code Playgroud) 我来自JavaScript,其中回调非常简单.我试图将它们实现到JAVA中,但没有成功.
我有一个Parent类:
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
ExecutorService workers = Executors.newFixedThreadPool(10);
private ServerConnections serverConnectionHandler;
public Server(int _address) {
System.out.println("Starting Server...");
serverConnectionHandler = new ServerConnections(_address);
serverConnectionHandler.newConnection = function(Socket _socket) {
System.out.println("A function of my child class was called.");
};
workers.execute(serverConnectionHandler);
System.out.println("Do something else...");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个从父级调用的子类:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ServerConnections implements Runnable {
private int serverPort;
private ServerSocket mainSocket;
public ServerConnections(int _serverPort) {
serverPort = _serverPort;
} …Run Code Online (Sandbox Code Playgroud) 我需要在Java中进行一些反射方法调用.这些调用将包含具有基本类型(int,double等)的参数的方法.反向查找方法时指定此类型的方法是int.class,double.class等.
挑战在于我接受来自外部源的输入,该源将动态指定类型.因此,我还需要动态地提出这些类引用.想象一下分隔文件的方法名列表以及参数类型列表:
doSomething int double
doSomethingElse java.lang.String boolean
Run Code Online (Sandbox Code Playgroud)
如果输入类似java.lang.String,我知道我可以使用Class.forName("java.lang.String")该类实例.有没有办法使用该方法或其他方法来获取原始类型Classes?
编辑:
感谢所有受访者.很明显,没有内置的方法来干净地做我想要的事情,所以我将决定重用ClassUtilsSpring框架中的类.它似乎包含Class.forName()的替代品,可以满足我的要求.
我是Java新手,但我需要编写类似C#代码的东西(这是手工打造的原型,只是为了说明我需要的东西)
private void ParentFunc()
{
var worker = new WorkerClass()
worker.DoWork(e => console.Write("Progress" + e));
}
public class WorkerClass()
{
public method DoWork(Action<int> callback)
{
for (int i=1; i<1000; i++) callback.Invoke(i);
}
}
Run Code Online (Sandbox Code Playgroud)
小解释.我AsyncTask在android中使用并在处理器外部进行调用,但希望它们能够发出信号,以便我可以publishProgress.我不想把界面放在我的身上AsyncTask
我在Java中遇到了关于回调的问题.她的是正在运行的代码和原来的答案在这里.
码:
public class Main {
public interface Visitor {
int DoJob(int a, int b);
}
public static void main(String[] args) {
Visitor adder = new Visitor(){
public int DoJob(int a, int b) {
return a + b;
}
};
Visitor multiplier = new Visitor(){
public int DoJob(int a, int b) {
return a*b;
}
};
System.out.println(adder.DoJob(10, 20));
System.out.println(multiplier.DoJob(10, 20));
}
}
Run Code Online (Sandbox Code Playgroud) 这是一个功能吗?
它是从源头调用的函数吗?
或者,它是从目的地返回的功能吗?
或者,它只是在目的地执行一个功能?
或者,它是从传递给目标的函数返回的值吗?
java ×5
callback ×3
android ×1
c# ×1
definition ×1
delegates ×1
java-8 ×1
primitive ×1
reflection ×1
types ×1