【问题标题】:How to call function in classdef matlab如何在classdef matlab中调用函数
【发布时间】:2014-05-31 16:11:12
【问题描述】:

我有一个类和一个函数,我想将函数放在类a中只是想在另一个类中调用整个,但是在调用时会出现一定的错误,是否可以调用函数而不在类中调用它构造函数?我目前正在调用类构造函数,但更有可能采用其他可能的方式。 func 中需要五个参数,我怎样才能使该函数成为一个类?

当输入参数给出obj.arg1=arg1;时,我也在构造函数中尝试过 我的代码:

classdef myClass
        properties 
            node;
        end
        properties (Access=private)

        end
        methods
        function obj = myClass()

        func(obj,obj,obj,obj,obj);
        end

        function  node = func(arg1,arg2,arg3,arg4,arg5)
          %some operation
        end
end

【问题讨论】:

    标签: matlab function class oop object


    【解决方案1】:

    您希望在不实例化类实例的情况下为那些要调用的函数设置一个单独的methods(Static) 部分。对于静态部分中的任何方法,您可以从另一个文件中执行:

    <some code here>
    answer = myClass.myStaticMethod(args);
    <rest of code here>
    

    而对于没有(Static) 的通用methods 块中的任何内容,您必须实例化该类,然后针对该实例调用方法,即:

    <some code here>
    classInstance = myClass(constructor args)
    answer = classInstance.myNonStaticMethod(args);
    <rest of code here>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 2020-08-15
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多