为什么使用方法本地抽象内部类
可以与方法局部内部类一起使用的合法修饰符之一是抽象的。
例如:
public class Outer {
public void method(){
abstract class Inner{
}
}
}
有没有什么情况你会实际使用这个?
为了参加 SCJP 考试,您必须了解这一点。
One of the legal modifiers you can use with method local inner classes is abstract.
For example:
public class Outer {
public void method(){
abstract class Inner{
}
}
}
Is there any situation where you would actually use this?
You have to know this for the SCJP exam.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
原始问题中有一些无效的假设。某些东西是合法/有效的 Java 并不意味着它是您需要使用或需要了解的东西。
我不记得 SCJP 包含奇怪的极端案例问题。
我试图想出一个案例,在该案例中我会使用在方法中声明的抽象类,但一切看起来都很奇怪,而且设计得很糟糕。
然而,这是我想出的一个代码示例(恕我直言,代码设计仍然很糟糕)
The are some invalid assumptions in the original question. That something is legal/valid Java doesn't mean that it is something that you need to use, or need to know.
I can't recall that the SCJP contains odd corner case questions.
I tried to come up with a case where I would have used an abstract class declared in a method, but everything looks very odd, and reeks of bad design.
Here's however a code example that I came up with (still bad code design IMHO)
我只能在这种情况下思考
但我无法想象真正的用法
I can think only in this case
But I can't imagine real usage
恕我直言,这个功能没有实际用途。有几种可能的滥用情况,但还有许多其他方法可以编写糟糕的代码,您无需学习这一方法。 :D
每当您尝试使用抽象方法局部类时,您需要定义至少两个具体方法内部类。这意味着您最终会得到一个至少包含三个类的方法,该方法变得相当长,这是一种非常糟糕的风格。
我真的希望不会。方法局部内部类已经毫无用处,足以被视为极端情况(您应该理解它们,但可能永远不会使用它们)。
恕我直言,一个在考试中问这个问题的人严重误解了 Java。本地类上不能有可访问性修饰符,因为(缺少方法文字)无论如何都无法从外部访问该类。可以有
abstract
和final
修饰符,因为没有理由禁止它们。允许它们有充分的理由:正交性和最小惊讶原则。IMHO, this feature has NO real use. There's a couple of possible abuses, but there are many other ways to write bad code, you needn't learn this one. :D
Whenever you try to make use of an abstract method-local class, you need to define at least two concrete method-inner classes. This means you end up with a method containing at least three classes, the method gets quite long and that's quite a bad style.
I really hope not. Method-local inner classes are already useless enough to be considered a corner case (you should understand them but probably never use them).
IMHO, a person asking this in an exam misunderstood Java badly. There can't be accessibility modifiers on a local class since (lacking method literals) the class can't be accessed from the outside anyway. There can be
abstract
andfinal
modifiers, since there's no reason to forbid them. There are good reasons to allow them: orthogonality and the Principle of least astonishment.让 S2 表示您需要本地类的所有情况。
您的问题的答案可以通过检查 S1 ∩ S2
相关问题:
澄清:我的观点是,这两个功能(抽象类和本地类)是该语言的两个完全正交的功能。了解每个功能何时有用是了解它们何时同时有用的关键。
Let S1 denote all situations in which you need an abstract class.
Let S2 denote all situations in which you need a local class.
The answer to your question can be found by examining S1 ∩ S2
Related questions:
Clarification: My point is that the two features (abstract classes and local classes) are two completely orthogonal features of the language. Understanding when each feature is useful is the key to understanding when they are both useful at the same time.
您可以在这里使用 http://java-questions.com/InnerClass_interview_questions.html
上面写着
在方法内部声明的内部类称为方法本地内部类。局部内部类的方法只能声明为final或abstract。方法局部类只能访问全局变量,或者声明为final的方法局部变量
,即可以在内部调用中声明静态变量并在方法中使用它们。
编辑:为什么抽象:
因为如果您不想创建内部类的对象。如果您在方法中创建对象,那么它将存储在堆中,并且即使方法执行完成也不会释放它,因为从方法返回时可能存在该对象的外部引用。
所以这取决于你是否要创建实例。如果您想创建,请使用 final 修饰符。
You can get the use here http://java-questions.com/InnerClass_interview_questions.html
which says
The inner class declared inside the method is called method local inner class. Method local inner class can only be declared as final or abstract. Method local class can only access global variables or method local variables if declared as final
ie You can declare the static variables in the inner call and use them in the methods.
EDIT: Why abstract:
Because if you dont want to create the objects of the inner class. If you create the object in the method then it will be stored in the heap and it is not freed even if the method execution completes as there might be an external reference for this object when it is returned from the method.
So it depends on whether you want to create an instance or not. If you want to create then use final modifier.
我能想象的唯一真正的用途是数据结构中的节点
,这样您就可以将方法与哨兵节点和普通数据节点区分开来,这在递归算法中非常方便,并且您不必每次都进行空检查
the only real use I can imagine is for nodes in a data structure
that way you can differentiate methods from sentinel nodes and normal data nodes which can be really handy in recursive algorithms and you don't have to null check each time
不,方法内部的抽象类(或一般类)没有什么用处。
只有当特定的方法需要特定的类并且也实现它时,它才有意义。实际上,这种情况可能在您编写的数万亿个方法中发生一次。
No, there is no good use for abstract classes (or classes in general) inside methods.
It would only make sense if only that particular method would need that particular class and would also implement it. Actually having that situation maybe happens once in trillions of methods you write.
查看此页面上标题为“内部类的层次结构”的部分。
要点是,您可以将内部类视为另一个需要重写/实现的抽象成员。我不一定同意它(我可能只是单独定义内部类),但我在野外见过这样的事情。
这是他们的示例代码:
Check out the section titled "Hierarchies of Inner Classes" on this page.
The gist is that you can treat the inner class as just another abstract member that needs to be overridden/implemented. I don't necessarily agree with it (I would probably just define the inner class separately), but I've seen things like this in the wild.
Here's their example code:
我认为在某些情况下减少方法的范围可能很有用。
例如,我在单元测试中使用它。有时您需要一个实用方法来减少测试的冗长性。但此实用方法可能与当前测试数据集相关,并且无法在本次测试之外重用。
在这个现实生活中的例子中,我本可以做一个常规的内部类,但有人可能会想在其他测试中重用它,但它的设计初衷并非如此。
顺便说一下,您会注意到直接在实用程序类中“捕获”测试中构建的数据集的能力。使用常规内部类,如果不在测试之外创建特定于测试的数据集,它就无法工作...因此,您最终会得到与其他测试共享的很多内容,而它们仅由一个测试使用(应该使用) 。
最后,我不认为允许降低可见性的功能是无用的。
您可以在根本不使用封装的情况下构建一个完美工作的应用程序,并且可以争论同样的事情,说 private 修饰符没用...
但是,是的,私有修饰符肯定比方法本地内部类更有用;)
I think it can be useful to reduce the scope of methods in certain conditions.
For exemple, I use it in unit tests. Sometimes you need an utility method to reduce the verbosity of a test. But this utility method may be related to the current test dataset, and can't be reused outside of this test.
In this real-life exemple, I could have done a regular inner class, but someone could have been tempted to reuse it in other tests, while it was not designed to.
By the way, you will notice the ability to "capture" the dataset build in the test directly inside the utility class. Using a regular inner class, it couldn't work without creating the test specific dataset outside the test too... so you end up with a lot of things shared with other tests, while they are used (should be used) by only one.
In the end, I don't think a feature permitting to reduce the visibility is useless.
You can build a perfectly working application without using encapsulation at all, and can argue the same thing, saying the private modifier is useless...
But yes, the private modifier is certainly more useful than method local innerclasses ;)
你可以像这样使用它。
You can use it like this.