问题描述
我想知道如何在MATLAB中获得对Java枚举
或静态公共字段的引用。在MATLAB中,如果您尝试使用Java对象/方法,那么Java对象创建/方法调用/ etc等同于: Java: new com.example.test.Foo();
MATLAB: javaObject('com.example.test foo');
Java: com.example.test.Foo.staticMethod();
MATLAB: javaMethod('staticMethod','com.example.test.Foo');
Java: SomeEnum e = com.example.test.SomeEnum.MY_FAVORITE_ENUM;
MATLAB:
Java: int n = com.example.test.Foo。 MAX_FOO
;
MATLAB:
与任何其他静态Java字段一样,您可以使用package.class.FIELD语法从Matlab引用Java枚举常量。假设你有一个枚举。
package com.example;
public enum MyEnum {
FOO,BAR,BAZ
}
您可以使用直接引用获取Matlab中的枚举常量。 (当然,Java类必须在Matlab的javaclasspath中。)
%静态引用
foo = com.example .MyEnum.FOO
%如果要省略包名,则导入它
import com.example.MyEnum;
foo = MyEnum.FOO
bar = MyEnum.BAR
如果你想在运行时确定的动态引用,您只需构建一个包含等效静态引用的字符串,并将其传递给eval()。这几乎可以用于任何Matlab代码。
%动态引用
foo = eval('com.example.MyEnum。 FOO')
如果你想要真的很喜欢,可以使用Java反射来获取枚举的常量在运行时。制作一个薄的包装器与你的其他自定义类来摆脱麻烦与Matlab的类加载器。 (没有Matlab javaClass()等效; IMHO这是一个Matlab监督。)
//在Java
包com。示例;
public class Reflector {
public static Class forName(String className)throws Exception {
return Class.forName(className);
}
}
然后你可以枚举Matlab中的常量。 p>
%使用反射的常量枚举
klass = com.example.Reflector.forName('com.example.MyEnum');
enums = klass.getEnumConstants();
I'm wondering how in MATLAB you can get a reference to a Java enum
or static public field. In MATLAB, if you are trying to use Java objects/methods, there are equivalents to Java object creation / method call / etc.:
Java: new com.example.test.Foo();
MATLAB: javaObject('com.example.test.Foo');
Java: com.example.test.Foo.staticMethod();
MATLAB: javaMethod('staticMethod', 'com.example.test.Foo');
Java: SomeEnum e = com.example.test.SomeEnum.MY_FAVORITE_ENUM;
MATLAB: ?????
Java: int n = com.example.test.Foo.MAX_FOO
;
MATLAB: ?????
You can reference Java enum constants from Matlab using the package.class.FIELD syntax, as with any other static Java field. Let's say you have an enum.
package com.example;
public enum MyEnum {
FOO, BAR, BAZ
}
You can get at the enum constants in Matlab using a direct reference. (The Java classes must be in Matlab's javaclasspath, of course.)
% Static reference
foo = com.example.MyEnum.FOO
% Import it if you want to omit the package name
import com.example.MyEnum;
foo = MyEnum.FOO
bar = MyEnum.BAR
If you want a "dynamic" reference determined at runtime, you can just build a string containing the equivalent static reference and pass it to eval(). This works on almost any Matlab code.
% Dynamic reference
foo = eval('com.example.MyEnum.FOO')
And if you want to get really fancy, you can use Java reflection to get at all the enumerated constants at run time. Make a thin wrapper to put with your other custom classes to get around quirks with Matlab's classloader. (There's no Matlab javaClass() equivalent; IMHO this is a Matlab oversight.)
//In Java
package com.example;
public class Reflector {
public static Class forName(String className) throws Exception {
return Class.forName(className);
}
}
Then you can enumerate the constants in Matlab.
% Constant enumeration using reflection
klass = com.example.Reflector.forName('com.example.MyEnum');
enums = klass.getEnumConstants();
这篇关于在MATLAB中使用Java枚举或公共静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!