如何在Matlab中为整个类执行导入? [英] How do I perform an import for the entire class in matlab?

查看:66
本文介绍了如何在Matlab中为整个类执行导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用多个功能的另一个包中的其他类的类.为此,我目前必须在每个函数中导​​入包:

I have a class that uses other classes from another package in multiple functions. In order to do this, I current have to import the package in each function:

classdef foo
    properties
        bar
    end
    methods
        function self = foo()
            foo.bar = 1;
        end

        function fun1(foo)
            import pkg.FooClass;
            val = pkg.FooClass(foo.bar);
        end
        function fun2(foo)
            import pkg.FooClass;
            val = FooClass.fun(foo.bar);
        end
    end
end

有没有办法为整个班级导入软件包?我正在寻找与其他语言类似的东西:

Is there a way to import packages for the entire class? I'm looking for something similar to other languages:

classdef foo
    import pkg.FooClass;
    properties
        bar
    end
    methods
        function self = foo()
            foo.bar = 1;
        end

        function fun1(foo)
            val = pkg.FooClass(foo.bar);
        end
        function fun2(foo)
            val = FooClass.fun(foo.bar);
        end
    end
end

推荐答案

不幸的是,

导入功能仅影响该功能的导入列表 在其中使用.

The import function only affects the import list of the function within which it is used.

因此,您将必须在各处指定完全限定名称,或在每个函数中进行导入.

So you will either have to specify the full qualified name everywhere, or do an import in each function.

这篇关于如何在Matlab中为整个类执行导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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