Java支持内部/本地/子方法吗?
这是我的代码。
public class SubFunction {
private String drawTribleX(){
return trible("X");
}
private String trible(String t){
return t + t + t;
}
public static void main(String[] args){
SubFunction o = new SubFunction();
System.out.println(o.drawTribleX());
}
}
我可以做这样的事情吗?
public class SubFunction {
private String drawTribleX(){
// *** move trible(t) inside drawTribleX() ***
private String trible(String t){
return t + t + t;
}
return trible("X");
}
public static void main(String[] args){
SubFunction o = new SubFunction();
System.out.println(o.drawTribleX());
}
}
谢谢。
This is my code.
public class SubFunction {
private String drawTribleX(){
return trible("X");
}
private String trible(String t){
return t + t + t;
}
public static void main(String[] args){
SubFunction o = new SubFunction();
System.out.println(o.drawTribleX());
}
}
Can I do something like this ?
public class SubFunction {
private String drawTribleX(){
// *** move trible(t) inside drawTribleX() ***
private String trible(String t){
return t + t + t;
}
return trible("X");
}
public static void main(String[] args){
SubFunction o = new SubFunction();
System.out.println(o.drawTribleX());
}
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新 2014-02-09:
JDK 8 引入了 lambda(匿名函数表达式),它允许您像这样解决它:
(JDK 7 及更低版本) strong>
不,Java不支持“直接”嵌套方法。 (大多数函数式语言都是如此,包括一些 JVM 语言,例如 Scala 和 Clojure!)
仅供参考;您可以定义本地类(方法内的类),因此确实编译
注意,尽管有一些本地类的限制
因此,正如您所看到的,在这些情况下,您的第一个选择(没有嵌套方法)更可取。
Update 2014-02-09:
JDK 8 introduced lambdas (anonymous function expressions) which allow you to solve it like this:
(JDK 7 and below)
No, Java does not support "directly" nested methods. (Most functional languages do though, including some JVM languages such as Scala and Clojure!)
Just for reference though; You can define local classes (classes within methods) so this does compile
Note though that there are some restrictions on local classes
So, as you can see, your first option (without nested methods) is preferable in these situations.
很简单——不。您不能将一个方法嵌套在另一个方法中。
如果您真的想要这样做,您可以在方法中定义类(奇怪的是,考虑到前面的限制),因此可以将您的方法包装在外部方法内的类中。
然而,这不是很惯用,一般做法似乎是有一个私有方法列表(在顶级类中),可能根据目的进行分组,并用注释块来划分组。
然而,如果您碰巧发现自己使用 Scala,您可以随心所欲地嵌套方法......
Quite simply - no. You can't nest a method within another method.
If you really want to do this, you can define classes within methods (strangely, given the previous restriction) and so could wrap your methods within a class within the outer method.
However, this isn't very idiomatic, and the general practice seems to be to have a list of private methods (in the top level class), perhaps grouped according to purpose and with comment blocks demarcating the groups.
If you happen to find yourself using Scala, however, you can nest methods away to your heart's content...
您也可以尝试这种方式,即匿名内部类。
you can try in this way as well which is anonymous inner class.
您可以使用匿名类。像这样的东西;
如果您愿意,您可以使用相同的接口定义不同类型的操作,并根据需要传递这些操作。
You could use an anonymous class. Something like this;
If you wanted you could define different types of operations with the same interface, and pass these around as you like.
可以在任何方法中编写 lambda。
例如,使用 lambda 创建递归方法:
像 Interface 这样的对象
lambda 方法表达式需要
递归
findRoot
方法来获取顶级Foo.parent
It is possible to write lambdas inside any method.
Using lambda for example to create a recursive method:
Expecting an object like
Interface for the lambda method expression
Recursive
findRoot
method to get the top levelFoo.parent