与超类和子类构造函数接口 [英] Interfacing with super and subclass constructors

查看:187
本文介绍了与超类和子类构造函数接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在麻烦的MATLAB文档中找到帮助和以前关于使用matlab继承和类构造函数来创建一个接口的问题。



而不是拖动我的代码,我可以缩小它如下:



A + MyPkg 有一个超类 Super 和几个子类 Sub1 Sub2 ...我的大多数属性和方法在Super中定义,使 Sub1 Sub2 只存在使用它们的构造函数用于简单的例程或者可能有一些方法从Super重载。



那么如何编写classdefs和构造函数来支持一个接口可以使用以下调用:

  a = MyPkg.Super(args).Sub1(args)
b = MyPkg。 Super(args).Sub1(args).Sub1Method

在这种情况下,除了Sub1相关的参数的可读性和组织。



欢迎提出问题。



编辑:



在考虑下面接受的答案和一些浏览之后,我得出的结论是,上面显示的接口并不是真正的OO的精神,对于我的数据分析应用程序,它更合适的方法,它将包括一个句柄类填充对象属性的对象或单元数组的构造函数。因为类是一个句柄类,所以可以使用它的方法来产生所需的方法。即以下

 %in + MyPkg\ 

classdef Super<句柄
属性
outputArray
end
方法

function self = Super(args)
self.outputArray = load_values(args);
end

function out = do_analysis(self,params)
%做一些分析
end

end
end

然后使用:

  data1 = MyPkg.Super(args)
%填充outputArray
analysis1 = data1.do_analysis(params)
$ p>

解决方案

对于你的问题,你不能使用继承。只有直接超类构造函数可以从子类调用,并且只有从子类可以调用超类构造函数。 参考



暴露这样的超类真的打破了继承的基础。也许你应该考虑另一个模型,也许是组合(有一个而不是是一个,如果你需要那种访问?)


I have had trouble finding help in the matlab documentation and previous questions about using matlab inheritance and class constructors to make an interface. To make it tidy, within a package.

Instead of dragging through my code I can condense it as follows:

A package +MyPkg has a superclass Super and a few subclasses Sub1 Sub2... Most of my properties and methods are defined in Super such that Sub1 and Sub2 really only exist to use their constructors for simple routines or perhaps a few methods overloaded from Super.

So how do I go about writing the classdefs and constructors to support an interface where I can use the following calls:

a = MyPkg.Super(args).Sub1(args)
b = MyPkg.Super(args).Sub1(args).Sub1Method

In this case I want to keep arguments related to Super apart from arguments related to Sub1 for readability and organization.

Questions are welcome.

EDIT:

After considering the accepted answer below and some browsing I reached the conclusion that the interface shown above is not really in the spirit of OO and, for my data analysis application of it a more proper way to approach it would consist of a handle class with a constructor that populates an object or cell array of object properties. Because the class is a handle class one can then use the methods on it to produce desired methods. i.e. the following

% in +MyPkg\

classdef Super < handle
    properties
        outputArray
    end
    methods

    function self = Super(args)
        self.outputArray=load_values(args);
    end

    function out = do_analysis(self,params)
        % do some analysis
    end

    end
end

Then to use this:

data1 = MyPkg.Super(args)
% Populate the outputArray
analysis1 = data1.do_analysis(params)

etc.,

Hope that helps someone else dealing with these issues

解决方案

With respect to your question, you can't if you use inheritance. Only direct superclass constructors can be called from subclasses and only from the subclass can you call the superclass constructor. Ref.

Exposing the superclass like that really breaks the fundamentals of inheritance. Maybe ou should be thinking of another model, maybe composition ("has a" instead of "is a"), if you need that kind of access?

这篇关于与超类和子类构造函数接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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