加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oop – 如何使抽象依赖属性起作用

发布时间:2020-12-14 04:47:00 所属栏目:百科 来源:网络整理
导读:我想在这样的抽象类中定义一个具有属性的接口 classdef A properties (Abstract = true) Valid; endend 像这样的这个接口的实现 classdef B A properties (Dependent = true) Valid; end methods function v = get.Valid(obj) v = 1; end endend 但是当我尝
我想在这样的抽象类中定义一个具有属性的接口

classdef A
    properties (Abstract = true)
        Valid;
    end
end

像这样的这个接口的实现

classdef B < A
    properties (Dependent = true)
        Valid;
    end
    methods
        function v = get.Valid(obj)
            v = 1;
        end
    end
end

但是当我尝试制作B的实例时,我得到以下错误

>> c = B()
??? Error using ==> B
The property 'Valid' restriction defined in class 'B' must match the property definition in base class 'B'.

谁能告诉我我做错了什么?

解决方法

尝试在基类中设置Dependent属性:

classdef A
    properties (Abstract = true,Dependent = true)
        Valid;
    end
end

根据documentation:

Concrete subclasses must redefine abstract properties without the
Abstract attribute set to true

我理解这一点的方式,子类属性属性必须与基类匹配(没有Abstract属性)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读