Main Content

matlab.lang.correction.ConvertToFunctionNotationCorrection 类

命名空间: matlab.lang.correction

通过转换为函数表示法更正错误

自 R2019b 起

描述

在不应使用圆点表示法调用其方法的类中使用 ConvertToFunctionNotationCorrection 对象。方法引发的 MException 对象可以使用 ConvertToFunctionNotationCorrection 实例以建议将圆点表示法转换为函数表示法语法来调用该方法。

创建对象

描述

示例

cfnc = matlab.lang.correction.ConvertToFunctionNotationCorrection(method) 会创建一项更正,建议将圆点表示法转换为函数表示法语法,以调用引发 MException 对象的 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 方法。isvalidmyClass 从其超类继承的方法之一。

myObject = myClass;
myObject.isvalid
Use function syntax to call the 'isvalid' method.

Did you mean:
>> isvalid(myObject)

版本历史记录

在 R2019b 中推出