- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个枚举类型类:
public enum Operation {
PLUS() {
@Override
double apply(double x, double y) {
// ERROR: Cannot make a static reference
// to the non-static method printMe()...
printMe(x);
return x + y;
}
};
private void printMe(double val) {
System.out.println("val = " + val);
}
abstract double apply(double x, double y);
}
正如你在上面看到的,我定义了一个 enum
类型,它的值是 PLUS
。它包含一个特定于常量的主体。在它的正文中,我尝试调用 printMe(val);
,但我得到了编译错误:
Cannot make a static reference to the non-static method printMe().
为什么会出现此错误?我的意思是我正在重写 PLUS
正文中的抽象方法。为什么它在 static
范围内?如何摆脱它?
我知道在 printMe(){...}
上添加 static
关键字可以解决问题,但我很想知道是否有其他方法保持 printMe()
非静态?
另一个问题,与上述问题非常相似,但这次错误消息听起来相反,即 PLUS(){...}
具有非静态上下文:
public enum Operation {
PLUS() {
// ERROR: the field "name" can not be declared static
// in a non-static inner type.
protected static String name = "someone";
@Override
double apply(double x, double y) {
return x + y;
}
};
abstract double apply(double x, double y);
}
我尝试声明一个 PLUS
特定的 static
变量,但最终出现错误:
the field "name" can not be declared static in a non-static inner type.
如果 PLUS
是匿名类,为什么我不能在 PLUS
中定义静态常量?这两条错误消息听起来相互矛盾,因为第一条错误消息说 PLUS(){...}
有 static 上下文,而第二条错误消息说 PLUS(){...}
具有非静态上下文。我现在更困惑了。
最佳答案
这是一个奇怪的案例。
看来问题是:
在这种情况下,私有(private)成员应该是可访问的(6.6.1。):
Otherwise, the member or constructor is declared
private
, and access is permitted if and only if it occurs within the body of the top level class that encloses the declaration of the member or constructor.
但是,私有(private)成员不会被继承 (8.2):
Members of a class that are declared
private
are not inherited by subclasses of that class.
因此,printMe
不是匿名子类的成员,编译器会在父类(super class)中搜索它* Operation
(15.12.1):
If there is an enclosing type declaration of which that method is a member, let T be the innermost such type declaration. The class or interface to search is T.
This search policy is called the "comb rule". It effectively looks for methods in a nested class's superclass hierarchy before looking for methods in an enclosing class and its superclass hierarchy.
这就是奇怪的地方。因为printMe
在一个也包含的类中找到PLUS
,调用该方法的对象被确定为 Operation
的封闭实例, 不存在 (15.12.4.1):
Otherwise, let T be the enclosing type declaration of which the method is a member, and let n be an integer such that T is the n'th lexically enclosing type declaration of the class whose declaration immediately contains the method invocation. The target reference is the n'th lexically enclosing instance of
this
.It is a compile-time error if the n'th lexically enclosing instance of
this
does not exist.
简而言之,因为 printMe
只是 Operation
的成员(而不是继承),编译器被迫调用 printMe
在一个不存在的外部实例上。
但是,该方法仍然可以访问,我们可以通过限定调用来找到它:
@Override
double apply(double x, double y) {
// now the superclass is searched
// but the target reference is definitely 'this'
// vvvvvv
super.printMe(x);
return x + y;
}
The two error messages sound contradictory to each other [...].
是的,这是该语言的一个令人困惑的方面。一方面,匿名类永远不是静态的(15.9.5),另一方面,匿名类表达式可以出现在静态上下文中,因此没有封闭实例(8.1.3) .
An anonymous class is always an inner class; it is never
static
.
An instance of an inner class
I
whose declaration occurs in a static context has no lexically enclosing instances.
为帮助理解其工作原理,以下是一个格式化示例:
class Example { public static void main(String... args) { new Object() { int i; void m() {} }; }}
<i>italics</i>
中的所有内容是静态上下文。从<b>bold</b>
中的表达式派生的匿名类被认为是内部的和非静态的(但没有 Example
的封闭实例)。
由于匿名类是非静态的,它不能声明静态非常量成员,尽管它本身是在静态上下文中声明的。
* 除了使事情变得模糊之外,Operation
的事实是一个枚举完全不相关(8.9.1):
The optional class body of an enum constant implicitly defines an anonymous class declaration that extends the immediately enclosing enum type. The class body is governed by the usual rules of anonymous classes [...].
关于java - 特定于枚举常量的类主体是静态的还是非静态的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28964926/
我试图将护照逻辑放入 Controller 文件中,但问题是当我将逻辑放入 Controller 中时,它告诉我“无法读取未定义的属性“主体””,但是当我将代码移至索引时,所有内容都会路由向右走 in
我正在学习 Javascript,我正在尝试创建一个简单的下拉菜单。我想要的功能的一个例子可以在谷歌主页的顶部菜单中看到,带有“更多”和“设置”下拉菜单。具体来说,当您单击关闭菜单时,菜单会消失。 我
我正在努力让 Swagger 正确呈现我的 ServiceStack 服务。 我希望看到一个 UserId 字符串作为表单参数,一个 PrivateCustomer 对象作为主体参数,但是尽管 Use
注意:由于随后的研究,这个问题已经完全重组。 我正在尝试从 Shiro 的主题 PrincipalCollection 中检索值.我在集合中添加了两个主体。 Username和 UUID .当我试图记
我们正在开发一个将 OAuth 2 用于两个用例的应用程序: 访问后端微服务(使用 client_credentials) 验证应用程序的用户(使用 authorization_code ,因此将用户
我有这段代码生成一个将 myNumber 乘以 5 的委托(delegate) ParameterExpression numParam = Expression.Parameter(typeof(i
我有一些jquery, $( document ).ready(function() { body=$(body).html; $("html").html(body); }); 这应
我创建了一个通用异常 DTO,它也扩展了 RuntimeException。通过这种方式,可以在应用程序中使用它,也可以将其用作 DTO。问题是当我将 DTO 应用于 ResponseEntity 构
在 Angular 5 HttpClient 中,我可以通过这种方式设置 HttpParams()。 const body = new HttpParams() .set('email', '
我正在从 RabbitMQ 读取数据,如下所示: connection = factory.newConnection(); ch = connection.createChannel() ; Str
如何使用不同类型的调用和响应主体来改造 PUT?我有一个错误限制。类型必须相同 and 。响应bodie可以包含int值,但call不应该,因为当我用int值初始化CallBody对象时,它已经包
原则上我想做这样的事情: #grab some value from outer source (i.e. file or list defined by another programer) set
我知道如何使用TextureRegions 创建动画并将其应用于非box2d 游戏中的对象。 但是在 libgdx 的 box2d 中,我不知道该怎么做。在CocosD2中,Sprite对象中有run
我有这段代码生成一个将 myNumber 乘以 5 的委托(delegate) ParameterExpression numParam = Expression.Parameter(typeof(i
我已经计算了花括号的数量,但无法弄清楚为什么类主体不完整。每次我试图修复类(class)时,都会把整个类(class)弄乱。问题出在代码中的最后一个类。最后一个花括号给我带来了类里面的麻烦。我正在使用
有人知道吗?我只能看到 ApplyTorque 和 SetAngularVelocity,我只想在将对象添加到模拟之前旋转对象,例如:所以我有一个 crate 倾斜靠在墙上,另一个 crate 是平的
我可以获得如何让图像出现在 box2d 主体上的简单答案吗?我尝试为图像和主体创建 x 和 y int,但是一旦主体移动,图像就会保持静态。如果您确实回答,请尽可能解释一下代码。如果您对我的完整源代码
我知道我可以通过使用 PolygonRegion 来做到这一点,但问题是我使用 scene2d.Stage 和几个 Actor 。您可能知道阶段使用 SpriteBatch 而我无法渲染 Polygo
您好,我有以下代码: function redirect(){ window.location.href='logged_out_chat.php'; } ...在我的标题和以下正文标记中:
我在 didBegin(contact:) 中触发了 SpriteKit 物理接触。我为要移出屏幕的 Dot 对象的实例抓取物理体,但是当我尝试像这样更改其位置时,没有任何反应: 第一种方法 /* I
我是一名优秀的程序员,十分优秀!