简体   繁体   English

创建实现Matlab接口的Java对象

[英]Create Java object that implements an interface from Matlab

For some reason, I am not able to create a Java object that implements a user-defined interface. 由于某些原因,我无法创建实现用户定义接口的Java对象。

I tried creating a Java object that implements a pre-defined interface and it worked fine. 我尝试创建一个实现预定义接口的Java对象,并且效果很好。

My interface: 我的界面:

public interface Speak 
{
    public void sayHello();
}

My Class: 我的课:

public class myPerson 
implements Speak
{
    public myPerson(String arg_firstName, int arg_age)
    {
        firstName = arg_firstName;
        age = arg_age;
    }

    public String firstName;
    public int age;

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
    }
}

For my class to work in eclipse, I had to export my interface as a .jar file, then I added it to the project libraries - and it worked just fine. 为了使我的班级能够在eclipse中工作,我必须将接口导出为.jar文件,然后将其添加到项目库中,并且效果很好。

My Matlab file: 我的Matlab文件:

clc
clear

javaclasspath('/path/to/Speak.jar');
javaclasspath('/path/to/myPerson.jar');

driver_1 = myPerson('Bob', 39);

The error that I'm getting is: 我得到的错误是:

Undefined function or variable 'myPerson'.

If I remove the implements interface, I can create the object just fine. 如果删除了implements接口,则可以创建对象。

I suggest you try 我建议你试试

javaclasspath({'/path/to/Speak.jar', '/path/to/myPerson.jar'});

(You need both Speak.jar and myPerson.jar on the classpath to instantiate a myPerson .) (在类路径上需要Speak.jarmyPerson.jar来实例化myPerson 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM