Java 匿名类作为实用函数?设计实际使用的参数,或一个参数(较大的对象)
情况是我必须在 Java 中使用函数指针来实现如此多的函数(所以我这样做了 way)并将每个匿名类保存到接口的静态变量中,以便我可以直接使用它们。
/** The Constant HARDLIM. */
public static final TransferePatternable HARDLIM =
new TransferePatternable() {
public DoubleMatrix transfere(DoubleMatrix netSum, double theta) {
return netSum.gt(theta);
}
public String getFuncName() {
return "HARDLIM";
}
};
但问题是,有时我不需要提供 Theta,所以如果我删除它,多态性将不起作用(10 个函数中有 2 个不需要 theta),所以我必须把它放在(现在的函数声明约定)丑陋)所以我想传递实际上包含 netsum 和 theta 的整个对象。
但我开始担心,因为它也会破坏这个功能的真正用途。所以最后我建议我将这些函数分开(非匿名),然后让匿名函数使用它们,但参数将是对象。就像下面这样:
/** The Constant HARDLIM. */
public static final TransferePatternable HARDLIM =
new TransferePatternable() {
public DoubleMatrix transfere(MyObject obj) {
return MyObjectUtilFun.hardlim(obj.getNetsum,obj.getTheta);
}
public String getFuncName() {
return "HARDLIM";
}
};
那么我采取的步骤正确吗?或者我在胡闹,请指导我!
The Situation is that I have to use Function pointers for so many functions in Java (so I did it this way) and saved each anonymous class to a static variable of the Interface, so that I could use them directly.
/** The Constant HARDLIM. */
public static final TransferePatternable HARDLIM =
new TransferePatternable() {
public DoubleMatrix transfere(DoubleMatrix netSum, double theta) {
return netSum.gt(theta);
}
public String getFuncName() {
return "HARDLIM";
}
};
But the problem is that sometimes I don't need to provide the Theta so if I remove it the polymorphism won't work, (2 Functions out of 10 don't need theta) so I had to put it (Function declaration conventions now ugly) so I thought of passing the whole Object which actually contains both netsum and theta.
But I'm starting to worry, cause it's also going to ruin what this function really is for. So at last I suggested I put these function separately (non anonymous) and then make anonymous functions use them but the argument would be the object. Like the following:
/** The Constant HARDLIM. */
public static final TransferePatternable HARDLIM =
new TransferePatternable() {
public DoubleMatrix transfere(MyObject obj) {
return MyObjectUtilFun.hardlim(obj.getNetsum,obj.getTheta);
}
public String getFuncName() {
return "HARDLIM";
}
};
So Am I taking the right steps ? or I'm messing around, Please guide me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您真的需要该实例是
public static final
吗?如果您可以在任何有 theta 引用的地方实例化该实例,那么您的匿名类就可以使用该 theta 引用。例如:或者,您可以将输入指定为通用类型,这样您的
MyObject
不必是所有可能输入的超集,而是可以通过 TransferePatternable 类型进行区分。显然这里的缺点是你需要知道你正在调用什么类型才能提供正确的输入,但如果你不想在某些情况下提供 theta,你无论如何都需要知道这一点。最后,此问题的另一种常见解决方案是仅用一个
Map
替换所有方法参数。然后,你可以传递任何你想要的东西!这有很多明显的缺点,但很多 API 正是这样做的,通常您会看到它们将地图称为“上下文”。下面是一些示例:javax.servlet .ServletRequest
s 在Map
中存储参数javax.interceptor.InitationContext
类Map
中地图
s几年前,我自己在用java实现类似Excel的公式语言时就使用过这个
Map
解决方案。这样的公式可以解析为函数和变量,并且在执行函数时,我们提供了一个Map
,其中包含由变量名称作为键控的变量。显然,您仍然需要了解您所调用的内容,事实上,我们始终对在Map
中提供正确输入的公式有足够的了解。但我必须再次提醒您:这种代码相当难以实现和维护。除非您预计随着时间的推移会增加大量功能,否则不要走这条路。它不是面向对象友好的,它应该是最后的手段。Do you really need the instance to be
public static final
? If you can instantiate the instance wherever you have a reference to theta, then your anonymous class can use that theta reference. For example:Alternatively you can specify the input as a generic type such that your
MyObject
need not be a superset of all possible inputs, but rather can differ by TransferePatternable type. Obviously the drawback here is that you need to know what type you're calling in order to provide the right input, but you sort of need to know this anyway if you don't want to provide theta in some situations.Finally, another common solution to this problem is to replace ALL method parameters with just one
Map
. Then, you can pass in whatever you want! This has lots of obvious drawbacks, but lots of APIs do exactly this, and generally you'll see them refer to the map as the "context". Here are a few examples:javax.servlet .ServletRequest
s store parameters in aMap
javax.interceptor.InvocationContext
classMap
of named javabeansMap
sI myself have used this
Map
solution when implementing an Excel-like formula language in java years ago. Such a formula can be parsed into functions and variables, and when executing the function we provided aMap
containing the variables keyed by variable name. Obviously you still need to know something about what you're invoking, and in fact we always did know enough about the formula that providing the right inputs in aMap
was easy. But again I have to caution you: this sort of code is fairly hard to implement and maintain. Unless you anticipate growing a large set of functions over time, don't go down this route. It's not OO-friendly, and it should be a last resort.如果
MyObject
是常用的接口或类,并且TransferePatternable
预计不会与其他任何东西一起使用,那么您的第二个想法是最好的。它开启了 TransferePatternable 不仅可以与 netSum 和 theta 一起使用的可能性,而且还可以消除不需要的 theta。我的猜测是,这就是您想要做的,即使这意味着扩展 MyObject 类/接口的功能、范围和重要性。但是您将 TransferePatternable 限制为与 MyObject 实例一起使用。未使用的 theta 是一个问题,但对于多态性的强大功能来说,这是一个很小的代价(而且它比大多数其他解决方案更简单、更简洁)。如果 MyObject 解决方案对您来说并不完美,请坚持使用未使用的 theta。我的猜测是,一个好主意迟早会出现,如果不出现也没有什么坏处。
If
MyObject
is a generally used interface or class andTransferePatternable
is not expected to work with anything else, your second idea is best. It opens up the possibilities of a TransferePatternable being able to work with more than just netSum and theta and gets rid of the unneeded theta. My guess is that this is what you want to do, even if it means expanding the capabilities and scope and importance of the MyObject class/interface.But you are restricting a TransferePatternable to working with a MyObject instance. The unused theta is a problem, but it's a small price to pay for the power of polymorphism (and its a lot simpler and neater than most other solutions). If the MyObject solution doesn't look perfect to you, stick with the unused theta. My guess is a good idea will come along sooner or later, with no harm done if it doesn't.
有什么原因不能在 HARDLIM 中使用重载的“transfere”函数吗?
Is there any reason you can't have an overloaded "transfere" function in the HARDLIM?
最后,我使用了第二种选择,但记住了一些注意事项:
我还发现,用不必要的参数让用户感到困惑的代价很高,因为应用程序已经很复杂,不需要再复杂了。
At the end I used The second choice but with some notes in mind:
I also found the price of confusing users with unnecessary arguments to be high cause the application is already complex no need to be more complicated.