自定义枚举类的显示 [英] Customize display of an enumeration class

查看:123
本文介绍了自定义枚举类的显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 matlab.mixin.CustomDisplay 自定义枚举类的显示。

I'd like to customize the display of an enumeration class using matlab.mixin.CustomDisplay.

如果我有一个常规(非枚举)类,如下所示:

If I have a regular (non-enumeration) class such as the following:

classdef test < handle & matlab.mixin.CustomDisplay
    properties
       value
    end
    methods
        function obj = test(value)
           obj.value = value;
        end
    end
    methods (Access = protected)
    function displayScalarObject(obj)
        disp(['hello ', num2str(obj.value)])
    end
    end
end

然后一切正常 - 例如,

then everything works fine - for example,

>> a = test(1)
a = 
hello 1

但是如果我有枚举类如下(注意添加枚举块):

But if I have an enumeration class such as the following (note the addition of the enumeration block):

classdef test < handle & matlab.mixin.CustomDisplay
    properties
       value
    end
    methods
        function obj = test(value)
           obj.value = value;
        end
    end
    methods (Access = protected)
    function displayScalarObject(obj)
        disp(['hello ', num2str(obj.value)])
    end
    end
    enumeration
        enum1(1)
    end
end

然后显示不是自定义的 - 例如,

then the display is not customized - for example,

>> a = test.enum1
a = 
    enum1

使用调试器,我可以看到我的 displayScalarObject 方法从未被调用。实现 matlab.mixin.CustomDisplay 的其他方法,如 displayNonScalarObject 等似乎没有帮助 - 这些从来没有得到调用。

Using the debugger, I can see that my displayScalarObject method is never called. Implementing other methods of matlab.mixin.CustomDisplay such as displayNonScalarObject and so on doesn't seem to help - these never get called either.

发生了什么事?对于枚举类,do disp 显示的工作方式不同,这样一来,任何被 matlab.mixin.CustomDisplay 只是被忽略?

What's going on? Do disp and display work differently for enumeration classes, in such a way that anything that's overridden by matlab.mixin.CustomDisplay just gets ignored?

有没有办法获得定制的显示与 matlab。 mixin.CustomDisplay ,但使用枚举类?

Is there a way to get a customized display with matlab.mixin.CustomDisplay, but using an enumeration class?

PS我可以直接超载 disp 和/或在不继承 matlab.mixin.CustomDisplay 的枚举类中显示,而这个工作正常。但是,如果可能,我希望使用 matlab.mixin.CustomDisplay

PS I am able to directly overload disp and/or display on an enumeration class that does not inherit from matlab.mixin.CustomDisplay, and this works fine. But I'm looking to use matlab.mixin.CustomDisplay if possible.

推荐答案

我会回答我自己的问题,以防其他人感兴趣。

I'll answer my own question in case anyone else is interested.

该问题是参考MATLAB R2014a提出的。在R2014b中,在 matlab.mixin.CustomDisplay 的文档中添加了一个附加注释,使其明确表示不可能使用 matlab。 mixin.CustomDisplay 为枚举类派生自定义显示,并表示最好的方法是超载 disp

The question was asked with reference to MATLAB R2014a. In R2014b, an additional note has been added to the documentation for matlab.mixin.CustomDisplay, making it explicit that it is not possible to use matlab.mixin.CustomDisplay to derive a custom display for enumeration classes, and suggesting that the best approach is to overload disp.

这篇关于自定义枚举类的显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆