Scala 访问 Java 方法时遇到问题

发布于 2024-11-03 14:45:38 字数 935 浏览 0 评论 0原文

所以,我有一些用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

熊抱啵儿 2024-11-10 14:45:39

你是否可以对班级进行一次测试,看看你是否答对了?

p.getClass.getMethods

...如果可能的话(可能会遇到 NPE)以便找到来源:

p.getClass.getProtectionDomain.getCodeSource.getLocation.getPath

Is it possible for you to run a test against the class just to see if you got the right one?

p.getClass.getMethods

... and if possible (may run into NPE) in order to find the source:

p.getClass.getProtectionDomain.getCodeSource.getLocation.getPath
尐偏执 2024-11-10 14:45:39

编译 Scala 类时,请执行以下操作:

scalac *.scala *.java

这样,Scala 将查看 Java 代码以查看可用的内容。但是,如果 Java 代码已编译并作为 jar 文件提供,只需将其添加到编译 Scala 代码时使用的类路径即可。

When compiling the Scala class, do something like this:

scalac *.scala *.java

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.

淡淡的优雅 2024-11-10 14:45:38

我从来没有遇到过这样的问题,而且您可能检查了您的配置和其他所有内容两次,所以我猜这是一些与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文