【问题标题】:Undefined function 'addListener'未定义的函数'addListener'
【发布时间】:2015-05-13 17:07:38
【问题描述】:

我正在阅读 Matlab OOP pdf 的 pdf 书,在实现银行帐户第 3 章的代码后,它抱怨了一些事情。

>> BA = BankAccount(1234567, 500)
    Undefined function 'addListener' for input arguments of type 'BankAccount'.
Error in AccountManager.addAccount (line 20)
            lh = addListener(BA, 'InsufficientFunds', @(src,
            ~)AccountManager.assignStatus(src))
Error in BankAccount (line 21)
            BA.AccountListener = AccountManager.addAccount(BA); 

我不知道为什么会这样,因为我遵循了给出的示例,如下所示:

classdef BankAccount < handle
    %UNTITLED Summary of this class goes here
    %   Detailed explanation goes here
    properties (Access = ?AccountManager)
    AccountStatus = 'open'
    end
    properties (SetAccess='private')
        AccountNumber
        AccountBalance
    end
    properties (Transient) %not saved
        AccountListener
    end
    events
        InsufficientFunds
    end
    methods
        function BA = BankAccount(AccountNumber, InitialBalance)
            BA.AccountNumber = AccountNumber;
            BA.AccountBalance = InitialBalance;
            BA.AccountListener = AccountManager.addAccount(BA);
        end

        function deposit(BA, amt)
            BA.AccountBalance = BA.AccountBalance + amt
            if BA.AccountBalance > 0
                BA.AccountStatus = 'open'
            end
        end

        function withdraw(BA, amt)
            if (strcmp(BA.AccountStatus, 'closed') && BA.AccountBalance <= 0)
                disp(['Account', num2str(BA.AccountNumber), 'has been closed'])
                return
            end
            newBal = BA.AccountBalance - amt
            BA.AccountBalance = newBal
            if newBal < 0
                notify(BA, 'InsufficientFunds')
            end
        end

        function getStatement(BA)
            disp('-----------')
            disp(['Account', num2str(BA.AccountNumber)])
            ab = sprintf('%0.2f', BA.AccountBalance)
            disp(['Current Balance', ab])
            disp(['Account Status', BA.AccountStatus])
            disp('-----------')
        end

    end
    methods (Static)
        function obj = loadObj(s)
            if isstruct(s)
                accNum = s.AccountNumber
                initBal = s.AccountBalance
                obj = BankAccount(accNum, initBal)
            else
                obj.AccountListener = AccountManager.addAccount(s)
            end
        end

    end


end

还有:

classdef AccountManager
    %UNTITLED2 Summary of this class goes here
    %   Detailed explanation goes here

    properties
    end

    methods (Static)
        function assignStatus(BA)
            if BA.AccountBalance < 0
                if BA.AccountBalance < -200
                    BA.AccountStatus = 'closed'
                else
                    BA.AccountStatus = 'overdrawn'
                end
            end
        end

        function lh = addAccount(BA)
            lh = addListener(BA, 'InsufficientFunds', @(src, ~)AccountManager.assignStatus(src))
        end

    end

end

那么谁能告诉我这里发生了什么?在 Matlab R2014a(8.3.0.532) 上。我认为我已经正确实施但没有复制粘贴,也许我忽略了一行。谢谢。

【问题讨论】:

  • 错误很明显。在您的AccountManager 类中没有这种称为addListener 的方法。你确定你复制了整个例子吗?
  • 是的,我在看,书上没有这种方法。 :)
  • 是的,有充分的理由。看我的回答。

标签: matlab class oop


【解决方案1】:

那是因为你拼写方法的方式有点不正确。它被称为addlistener - 小写l。拼写时有一个大写 L。

您引用的书是 MATLAB 的官方面向对象编程指南 - http://www.mathworks.com/help/pdf_doc/matlab/matlab_oop.pdf。有问题的代码在第 3-16 页。

请记住,MATLAB 区分大小写。即使字符不同大小写,也会被解释为不同的变量、函数等。别担心——我认为addListener 更自然,因为有两个词。 addlistener 只是……很奇怪!

【讨论】:

  • 就是这样,愚蠢的错误我已经习惯了 XCode 的大写字母编程方式
  • @Pedro.Alonso - 哈哈,不用担心。 FWIW,我更喜欢addListener....addlistener 很奇怪。顺便说一句,如果我帮助了你,当你最终让它工作时,我不介意你接受我的回答:) 祝你好运!
  • 就是这样,愚蠢的错误我已经习惯了 XCode 的大写字母编程方式。谢谢
  • @Pedro.Alonso - 感谢您的接受!我喜欢addListener顺便说一句哈哈。 addlistener 看起来很奇怪。
  • 是的,确实看起来很奇怪,有 2 个词添加听众,好吧,我会把它扔到不好的地方:)
猜你喜欢
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
相关资源
最近更新 更多