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

我想在这样的抽象类中定义一个具有属性的接口

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属性)

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...