matlab.lang.correction.ConvertToFunctionNotationCorrection 类
命名空间: matlab.lang.correction
描述
在不应使用圆点表示法调用其方法的类中使用 ConvertToFunctionNotationCorrection
对象。方法引发的 MException
对象可以使用 ConvertToFunctionNotationCorrection
实例以建议将圆点表示法转换为函数表示法语法来调用该方法。
创建对象
描述
输入参量
method
— 方法的名称
字符串标量 | 字符向量
不正确地使用圆点表示法调用的方法的名称,指定为字符串标量或字符向量。method
必须为一个有效的 MATLAB® 标识符。有效的 MATLAB 标识符是由字母数字(A–Z、a–z、0–9)和下划线构成的字符串标量或字符向量,其中第一个字符应为字母且文本长度须小于或等于 namelengthmax
。
示例
在使用圆点表示法调用方法时建议修复
创建一个不应使用圆点表示法调用其方法的类。每当使用圆点表示法调用该类的方法时,均建议使用函数表示法语法。
在您的当前文件夹中,通过子类化 handle
超类来创建 myClass
类。在 methods
块中,重载 subsref
,将对 myClass
对象的调用方法限制为函数表示法语法。要在使用圆点表示法调用方法时在错误消息中添加建议的语法,请在 subsref
方法中使用 ConvertToFunctionNotationCorrection
实例。
classdef myClass < handle properties myProperty end methods (Hidden) function ref = subsref(obj, idx) firstSubs = idx(1).subs; if idx(1).type ~= "." || any(string(firstSubs) == properties(obj)) % Parentheses indexing, brace indexing, or property indexing try ref = builtin('subsref', obj, idx); return catch me end elseif any(string(firstSubs) == methods(obj)) % Valid method called using dot notation me = MException('myClass:useFunctionForm', ... 'Use function syntax to call the ''%s'' method.', ... firstSubs); cfnc = matlab.lang.correction.ConvertToFunctionNotationCorrection(firstSubs); me = me.addCorrection(cfnc); else % Invalid method, property, or field called using dot notation me = MException('MATLAB:noSuchMethodOrField', ... 'Unrecognized method, property, or field ''%s'' for class ''%s''.', ... firstSubs, class(obj)); end throwAsCaller(me) end end end
创建 myClass
的实例,并使用圆点表示法调用 isvalid
方法。isvalid
是 myClass
从其超类继承的方法之一。
myObject = myClass; myObject.isvalid
Use function syntax to call the 'isvalid' method.
Did you mean:
>> isvalid(myObject)
版本历史记录
在 R2019b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)