如何编写通用函数来操纵私有属性? [英] How can I write generalized functions to manipulate private properties?

查看:94
本文介绍了如何编写通用函数来操纵私有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,我想对类的私有成员执行一些操作.我也想在其他类上执行完全相同的任务.显而易见的解决方案是在一个单独的M文件中编写一个函数,所有类都调用该函数以执行此任务.但是,这在Matlab中似乎是不可能的(见下文).还有另一种方法可以做到这一点吗?

In Matlab, I would like to perform some operations on private members of a class. I would also like to perform this exact same task on other classes as well. The obvious solution is to write a function in a separate M file that all the classes call in order to perform this task. However, that seems impossible in Matlab (see below). Is there another way to accomplish this?

具体是这里的问题:假设我有一个包含内容的m文件

Here is the problem specifically: suppose I have one m file with the contents

classdef PrivateTest
    properties (Access=private)
        a
    end

    methods
        function this = directWrite(this, v)
            this.a = v;
        end
        function this = sameFileWrite(this, v)
            this = writePrivate(this, v);
        end
        function this = otherFileWrite(this, v)
            this = otherFileWritePrivate(this, v);
        end
        function print(this)
            disp(this.a);
        end
    end
end

function this = writePrivate(this, v)
    this.a = v;
end

...以及包含内容的另一个m文件

...and another m file with the contents

function this = otherFileWritePrivate(this, v)
    this.a = v;
end

实例化p = PrivateTest之后,这两个命令都可以正常工作(按预期方式):

After instantiating p = PrivateTest, both of these commands work fine (as expected):

p = p.directWrite(1);
p = p.sameFileWrite(2);

...但是此命令即使是相同的代码也无法使用,只是在不同的m文件中:

...but this command doesn't work even though it's the same code, just in a different m file:

p = p.otherFileWrite(3);

因此,似乎任何对类的私有属性执行操作的代码都必须与定义这些私有属性的classdef位于同一m文件中.另一种可能是让所有类都使用write方法继承一个类,但是Matlab也不允许这样做.在一个m文件中,我将获得以下代码:

So, it seems like any code that performs operations on private properties of a class MUST be in the same m file as the classdef that defines those private properties. Another possibility might be to have all the classes inherit a class with the writing method, but Matlab doesn't allow this either. In one m file, I would have this code:

classdef WriteableA
    methods
        function this = inheritWrite(this, v)
            this.a = v;
        end
    end
end

...然后在另一个m文件中,我将获得以下代码:

...and in another m file I would have this code:

classdef PrivateTestInherit < WriteableA
    properties (Access=private)
        a
    end
end

但是,实例化p = PrivateTestInherit;之后,命令p.inheritWrite(4)会导致与以前相同的错误消息:不允许设置'PrivateTestInherit'类的'a'属性."

However, after instantiating p = PrivateTestInherit;, the command p.inheritWrite(4) causes the same error message as before: "Setting the 'a' property of the 'PrivateTestInherit' class is not allowed."

有鉴于此,如何在Matlab中归纳操纵私有属性的代码,或者有可能?

In light of this, How is it possible to generalize code that manipulates private properties in Matlab, or is it possible?

推荐答案

无法在类外部操作私有属性,这就是为什么将它们称为私有的原因.这个想法称为封装.

It is not possible to manipulate private properties outside of the class, that is why they are called private. This idea is called encapsulation.

您可以通过多种方式解决该问题:

You can in solve it in many ways:

  1. 定义包装私有的公共属性,并对其进行更改. (请参见下面的代码)
  2. (继承模式)做一个普通的父类,您的其他类继承该函数


classdef PrivateTest
    properties (Access=private)
        a
    end

    properties(Access=public,Dependent)
        A
    end

    methods
        function this = set.A(this,val)
            this.a = val;
        end

        function val = get.A(this)
           val = this.a;
        end

        function this = directWrite(this, v)
            this.a = v;
        end
        function this = sameFileWrite(this, v)
            this = writePrivate(this, v);
        end
        function this = otherFileWrite(this, v)
            this = otherFileWritePrivate(this, v);
        end
        function print(this)
            disp(this.a);
        end
    end
end

function this = writePrivate(this, v)
    this.A = v;
end

这篇关于如何编写通用函数来操纵私有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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