Scala 访问 Java 方法时遇到问题
所以,我有一些用 Java 编写的东西,我想在 Scala 中扩展它......我遇到的问题是 Scala 没有看到我需要的方法。
它的设置方式如下: Player 扩展了 Mob,Mob 扩展了 Entity。
我需要访问 Player 中未在 Mob 或 Entity 中定义的方法,但 Scala 认为它不存在,尽管 Java 存在。
它可以很好地查看 Mob 和 Entity 定义的方法。另外,我所说的所有方法都是非静态的。
那么,我做错了什么,还是这是 Scala 施加的限制?
编辑 - 这是相关代码:
package test
import rsca.gs.model.Player
object Test {
def handle(p:Player): Unit = {
p.getActionSender().sendTeleBubble(0, 0, false);
}
}
玩家类别:
package rsca.gs.model;
// imports
public final class Player extends Mob {
// Implemented methods (not going to post them, as there are quite a few)
// Relevant code
private MiscPacketBuilder actionSender;
public MiscPacketBuilder getActionSender() {
return actionSender;
}
}
错误: 值 getActionSender 不是 rsca.gs.model.Player 的成员
So, I have something written in Java, and I want to extend it in Scala... The issue I'm running into is that Scala isn't seeing methods I need.
Here is how it's set up:
Player extends Mob, and Mob extends Entity.
I need to access a method in Player that isn't defined in Mob or Entity, but Scala doesn't think it exists even though Java does.
It can see methods defined by Mob and Entity just fine. Also, all the methods I'm talking about are non-static.
So, am I doing something wrong, or is this a limitation imposed by Scala?
Edit --
Here is the relevant code:
package test
import rsca.gs.model.Player
object Test {
def handle(p:Player): Unit = {
p.getActionSender().sendTeleBubble(0, 0, false);
}
}
Player class:
package rsca.gs.model;
// imports
public final class Player extends Mob {
// Implemented methods (not going to post them, as there are quite a few)
// Relevant code
private MiscPacketBuilder actionSender;
public MiscPacketBuilder getActionSender() {
return actionSender;
}
}
Error:
value getActionSender is not a member of rsca.gs.model.Player
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是否可以对班级进行一次测试,看看你是否答对了?
...如果可能的话(可能会遇到 NPE)以便找到来源:
Is it possible for you to run a test against the class just to see if you got the right one?
... and if possible (may run into NPE) in order to find the source:
编译 Scala 类时,请执行以下操作:
这样,Scala 将查看 Java 代码以查看可用的内容。但是,如果 Java 代码已编译并作为 jar 文件提供,只需将其添加到编译 Scala 代码时使用的类路径即可。
When compiling the Scala class, do something like this:
This way, Scala will look a the Java code to see what is available. If, however, the Java code is already compiled and provided as a jar file, just add it to the classpath used when compiling the Scala code.
我从来没有遇到过这样的问题,而且您可能检查了您的配置和其他所有内容两次,所以我猜这是一些与 Eclipse 相关的构建问题。您应该尝试从命令行进行构建,以查看是 Scala 还是 Eclipse 存在问题。
I never encountered such problems, and you probably checked your configuration and everything else twice, so I would guess this is some Eclipse related build issue. You should try to build from the command line in order to see whether Scala or Eclipse is the problem.