在一般情况下,有可能做这样的事情,但我会建议考虑一下。这个实现有点笨拙。
首先,您正在处理优先队列。这意味着你想要通过优先顺序接收添加到它的东西。优先队列是有序集合。为了做到这一点,你放入它的物品必须是Comparable
。这就是String或Integer的优先队列工作的原因。它们都是Comparable
- 如果队列中有两个字符串,则很容易判断哪个字符先出现,因为字符串具有自然顺序。尽管如此,必须说在现实生活中,你通常不会分配字符串优先级。优先队列应该表示哪些事情处理得更紧急,哪些事情更少。所以通常优先级更可能被实现为某种数字或枚举。
因此,除了在队列中放入函数之外,还需要在将队列放入队列时为其分配优先级,并且这将告知谁正在从队列中读取哪些函数更加紧迫。他只会按优先顺序排列。
因此,我们需要定义某种物体,将封装两种功能:和优先级:
public final class PrioritizedPerformer implements Comparable<PrioritizedPerformer> {
/**
* Actor interface. Represents a way to run a function.
*/
public interface Actor {
void perform();
}
/**
* Available priorities
*/
public enum Priority { URGENT, HIGH, MEDIUM, LOW };
private Priority priority;
private Actor actor;
/**
* Constructor that allows creating PrioritizedPerformer objects with a given priority and actor.
*
* @param priority Priority of this object when placed in a priority queue.
* @param actor Actor representing a function to be performed.
*/
public PrioritizedPerformer(Priority priority, Actor actor) {
// Don't allow null priorities, this will cause problems in priority queues.
if (priority == null) {
throw new IllegalArgumentException("Must provide valid priority");
}
this.priority = priority;
// Don't allow null actors. The action has to be available.
if (actor == null) {
throw new IllegalArgumentException("Must provide valid actor");
}
this.actor = actor;
}
/* Allows prioritizing one PrioritizedPerformer over another.
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(PrioritizedPerformer o) {
return priority.compareTo(o.priority);
}
/**
* Perform whatever action the actor represents.
*/
public void perform() {
actor.perform();
}
}
现在我们有了这一点,我们可以把这个类型的对象为优先级队列:
PriorityQueue<PrioritizedPerformer> queue = new PriorityQueue<PrioritizedPerformer>();
queue.offer(new PrioritizedPerformer(
PrioritizedPerformer.Priority.HIGH,
new PrioritizedPerformer.Actor() {
public void perform() {
login();
}
}));
queue.offer(new PrioritizedPerformer(
PrioritizedPerformer.Priority.LOW,
new PrioritizedPerformer.Actor() {
public void perform() {
quit();
}
}));
queue.offer(new PrioritizedPerformer(
PrioritizedPerformer.Priority.URGENT,
new PrioritizedPerformer.Actor() {
public void perform() {
returnThree();
}
}));
我们在这里做的是建立各部PrioritizedPerformer
对象与给定Priority
和匿名Actor
执行我们想让它执行的功能。
现在我们可以运行:
queue.remove().perform();
这会从队列中删除优先级最高的对象,并运行其perform()
方法,它调用其内部的Actor
perform()
方法。在这种情况下,它将采用优先级为URGENT
的驱动程序,该驱动程序恰好运行功能returnThree()
。
所以这将做你所要求的。但我反对它。它只能执行没有参数的函数(或者仅仅调用具有预定参数的特定函数)。你不能通过它像Math.sqrt
,因为当你需要执行它时,你无法将数字传递给该函数。
这会导致使用全局范围字段将数据传递给各种函数而不是参数。这反过来导致可读性问题,缺少封装,这意味着您的参数可能会被您不打算改变它们的方法改变。这是各种各样的不好。另外,在这个特定的设置中,如果您不知道下一步将运行哪个函数,您将如何知道要在全局范围中设置哪些参数?
Java不是JavaScript,函数(Java中的“方法”)不是可以分配给变量(甚至是匿名变量)的东西,然后传递。编程模型相当不同,所以你可以花一个小时阅读官方的[Java基础知识](http://docs.oracle.com/javase/tutorial/java/index.html)。不需要那么长时间,并且在理解Java可以,不可以,不想做和不可以做的事情方面给你一个很大的提升。 – 2014-11-04 17:52:16
其实java可以做我问的问题,有人确实回答了我的需求(指向最佳答案)谢谢:) – Indigo 2014-11-04 18:00:26
实际上,它不能,你得到的答案是关于创建一个新的Runnable实例,它被调用来运行与调用方法的方式不同。它可以做你想做的事,但你想做的是打破不少Java最佳实践和约定=) – 2014-11-04 18:02:32