问题标签 [local-class]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - Java中的内部局部类
我研究过的书说,本地类只能使用final
本地类所在的方法的变量和引用。在这个例子中,我使用b
了不是final
或引用的变量。它运行了,我没有收到任何错误。如何?有人可以解释这种行为吗?
java - 无法在 Eclipse 中观看 java 本地类实例
这是一个小样本:
我在最后一行放了一个断点。我可以在“变量”窗口中查看 util ...
我尝试在表达式窗口中查看相同的变量...它无法评估:
更新:
甚至尝试检查显示视图中的变量......它不会评估:
java - 使用反射检索方法内的类
我正在使用反射声明类,例如:
但是,我的程序找不到class inside
.
c++ - 模板函数派生本地类中的变量查找
我正在尝试使用 C++ 中的本地类并坚持使用以下代码:
为什么会这样?我是对的,这是编译器错误(我使用的是 gcc 4.8.4 和 gcc 4.9.2)并且代码是否正确?
java - 用构造函数体定义的本地类是否有问题
我有以下枚举:
问题是我从未在构造函数体内使用过本地类。它会导致一些错误,这样做不好吗?此外,构造函数 enu 的构造函数。
java - 为什么扩展内部类的本地类不能访问内部类封闭实例?
(我一直在重新阅读该问题的标题并思考它看起来有多荒谬,但我向你保证这是对问题的最佳描述,并且我有一个实际应用程序,这是最好的结构。我发誓我不是疯狂的。)
考虑以下。每个块都是一个单独的文件:
请注意,enclosingInstance
业务是解决涉及中间构造函数调用的问题。请参阅“为什么外部类不能扩展内部类?” .
我希望输出为“2”。但相反,我有一个编译错误System.out.println(A.this.i);
:
范围内无法访问类型 A 的封闭实例
我认为我试图解决的编程概念是合理的:在内部创建一种新类型的 Bmain
以提供给 A,它使用 B 类型可以访问的 A 中的东西。
那么我做错了什么,或者为什么这在java中不可能?
编辑/更新:请注意,将代码main
移至非静态方法时会出现相同的错误。也就是说,我尝试将其中的所有内容移动static void main
到一个新的、非静态方法被class Main
调用go()
。然后我改为static void main
单行new Main().go();
。错误在同一个地方。所以这似乎不是class C
在静态上下文中定义的问题。
java - 关于本地类的矛盾陈述,哪一个是正确的?
我是 Java 新手,正在尝试学习本地类的概念。我目前正在阅读官方 Java 文档 Oracle 上关于本地类的章节。我在本章中遇到了两个似乎相互矛盾的陈述。有知识的人可以帮我看看哪种说法是正确的吗?或者他们都是正确的,只是我对这个概念的理解还不存在。
本地类可以访问局部变量。但是,局部类只能访问声明为 final 的局部变量。
本地类是非静态的,因为它们可以访问封闭块的实例成员。
https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html
java - 什么调用 HelloWorldAnonymousClasses 中的 greet() 方法?
在 HelloWorldAnonymousClasses 示例程序中(来自此处):
什么调用对象的greet()
方法englishGreeting
?我很困惑..
编辑
感谢您的回复。我应该发现的!我试图解决另一个查询,即:
在 https://gist.github.com/bernii/5697073上的示例程序中
在代码中
什么调用匿名内部类中的 apply() 方法?
编辑
我现在在一个新问题中问了我的第二个查询。
java - Can we say that a method-local class is a type of Inner class?
Since we can not use the static
modifier with a local class defined inside a method, and since Nonstatic nested classes are Inner classes, we could probably say that a method local class is a type of an Inner class.
But on the other hand, we say that instances of Inner classes CAN NOT exist without a Live instance of the enclosing class. But an instance of a method-local class defined in a static method CAN exist without a live instance of the enclosing class, right?
So what do I conclude from this? The first logic tells me that local classes are a type of Inner class, and the second piece of reasoning tells me that Local class is not an Inner class.