Matlab 未定义函数';addListener';
我正在阅读Matlab OOP pdf的pdf手册,在实现了银行账户第3章的代码之后,它抱怨了一些事情Matlab 未定义函数';addListener';,matlab,class,oop,Matlab,Class,Oop,我正在阅读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,
>> 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银行账户0
BA.AccountStatus='open'
结束
结束
功能提取(BA、amt)
if(strcmp(BA.AccountStatus,'closed')&&BA.AccountBalance,这是因为您拼写该方法的方式有点不正确。它被称为-小写l
。拼写它时,您有一个大写的l
您正在参考的书是MATLAB的官方面向对象编程指南-。相关代码在第3-16页
请记住,MATLAB是区分大小写的。即使字符是不同的大小写,它也会被解释为不同的变量、函数等。别担心-我认为addListener
更自然,因为有两个词。addListener
只是…奇怪!错误很明显。没有称为ad的方法dListener
在您的AccountManager
类中。您确定完整复制了示例吗?是的,我正在查看,书中没有这样的方法。:)是的,有充分的理由。看我的答案。就是这样,愚蠢的错误,我习惯了XCode的字母编程方式capitalized@Pedro.Alonso-哈哈,别担心。FWIW,我更喜欢addListener
addlistener
很奇怪。顺便说一句,如果我曾经帮助过你,当你最终成功的时候,我不会介意你接受我的回答:)祝你好运!就是这样,愚蠢的错误,我习惯了XCode的大写字母编程方式。Thanks@Pedro.Alonso-谢谢你的接受!我确实很喜欢添加listener
btw lol.addListener
看起来很奇怪。是的,看起来确实很奇怪有两个词add listener,好吧,我会把它扔掉:)
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