从Matlab调用Java时如何避免歧义? [英] How to avoid ambiguity when calling Java from Matlab?

查看:177
本文介绍了从Matlab调用Java时如何避免歧义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现从Matlab调用Java时

I just discovered that when calling Java from Matlab

object.method(arg1,...,argn)

相当于

method(object, arg1,...,argn)

这里的问题是我还有一个 method.m ,它从Java到Matlab进行一些翻译(例如将 String [] 转换为单元格字符串)。我的 method.m 看起来像

The problem here is I also have a method.m that does some translation from Java to Matlab (eg. convert String[] to cell of strings). My method.m looks like

function result = method(object, arg1,...argn)
  intermediate = object.method(arg1,...argn);
  result = translate(intermediate);

当我调用方法(对象,arg1,.. 。,argn),它执行直接Java调用,而不是使用我的 method.m

What is happening is when I call method(object, arg1,...,argn), it does the direct Java call, instead of my using my method.m

修复很简单,只是不要对我的Java方法和我的 .m 文件使用相同的方法名称。 但还有另外一种方法吗?如何知道在同名的情况下调用哪种方法?有没有办法确保我调用 method.m 而不是Java方法?它很容易确保调用Java方法,只需使用 object.method 语法。

The fix is easy, just don't use the same method name for both my Java methods and my .m files. But is there another way? How do I know which method will be called given the same name? Is there a way to ensure I call method.m instead of the Java method? Its easy to ensure a call to the Java method, just use the object.method syntax.

作为旁注,愚蠢的是 .m 编辑器链接到方法上的 method.m (对象,arg1,...,argn)调用,而在调试时调用Java方法。

As a side note, what is also silly is the .m Editor links to the method.m on the method(object, arg1,...,argn) call, while when it debugs it calls the Java method.

推荐答案

你可能遇到了一些问题MATLAB会调度...

You may be running into some problems with how MATLAB does dispatching...

如何知道在同名的情况下调用哪种方法?

这MATLAB文档的第节讨论了在有多个具有相同名称的函数的情况下如何选择函数。从文档:函数优先顺序根据函数的类型及其在MATLAB路径上的位置确定一个函数优先于另一个函数。此订单(从最高到最低)如下:

This section of the MATLAB documentation discusses how a function is chosen in cases when there are multiple functions with the same name. From the documentation: "The function precedence order determines the precedence of one function over another based on the type of function and its location on the MATLAB path." This order (from highest to lowest) is given below:


  • 子功能

  • 私人功能

  • 类构造函数

  • 重载方法

  • 当前目录中的函数

  • 路径上其他位置的函数

  • Subfunction
  • Private function
  • Class constructor
  • Overloaded method
  • Function in current directory
  • Function elsewhere on the path

method.m函数的位置可能会决定它是被调用还是Java方法获得使用method(object,...)语法时调用。

The placement of your "method.m" function will likely determine if it gets called or the Java method gets called when using the "method(object,...)" syntax.

有没有办法确保我调用method.m而不是Java方法?

现在,我猜你的method.m在当前目录或路径的其他位置(两个最低优先级位置到在...)。如果你制作了method.m子功能在较大的代码中调用它,或者是否可以将它放在私有目录,每个需要调用它的函数都可以调用它,然后当你使用方法时,它可能被称为而不是 Java方法对象,...)语法。

Right now, I'm guessing your "method.m" is in the current directory or elsewhere on the path (the two lowest precedence positions to be in). If you made "method.m" a subfunction in the larger code calling it, or if it's possible to put it in a private directory where it can be called by every function that needs to call it, then it may get called instead of the Java method when you use the "method(object,...)" syntax.

希望这会有所帮助!

这篇关于从Matlab调用Java时如何避免歧义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆