如何获取MATLAB类中的静态成员变量? [英] How to obtain static member variables in MATLAB classes?

查看:1660
本文介绍了如何获取MATLAB类中的静态成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在MATLAB类中定义静态成员变量?

Is there a way to define static member variables in MATLAB classes?

这不起作用:

classdef A

    properties ( Static )
        m = 0;
    end
end

建议使用关键字Constant静态,常量属性不能修改。我想要一个变量与类 A 的所有对象共有,我想能够在类 A

It suggests to use keyword "Constant" instead of "Static", the constant properties cannot be modified. I want a variable common to all objects of class A and I want to be able to modify that variable in methods of class A.

所以我需要一个私有的静态成员变量。有没有办法在MATLAB中获得它?

So what I need is a private static member variable. Is there a way to obtain it in MATLAB?

找到一个解决方法可以完成

Found out that a workaround can be done using persistent variables in static member functions.

在这种情况下,你应该从一个基类继承所有的类,如下所示。

In this case you should inherit all your classes from a base class like the following.

classdef object < handle

    properties ( GetAccess = 'public', SetAccess = 'private' )
        id
    end

    methods ( Access = 'protected' )
        function obj = object()
            obj.id = object.increment();
        end
    end

    methods ( Static, Access = 'private' )
        function result = increment()
            persistent stamp;
            if isempty( stamp )
                stamp = 0;
            end
            stamp = stamp + uint32(1);
            result = stamp;
        end
    end  
end


推荐答案

你不能,它是设计。您应该使用持久性变量(MATLAB中的技术,1980年应用于2011年)!

You can not, it is by design. You should use a persistent variable (technique from the MATLAB as 1980 applied in year 2011)!

应该提到,实际上到2010年b一个未记录的,可能不再支持 static 属性修饰符。

For completeness I should mention that actually there is as of 2010b an undocumented and probably not longer supported static property modifier.

请参阅这里 Dave Foti ,MATLAB OO小组经理:

For background see here the answer of Dave Foti, MATLAB OO group manager:


在MATLAB中,类可以定义Constant
属性,而不是static
属性,在其他
语言(如C ++)的意义上。有beta
版本试验了
静态属性和
undocumented属性仍然从
。但是,静态属性是
未记录,不应该使用,并且
可能会在未来
MATLAB版本中删除。 R2008a实现它
作为Constant的同义词并提供
除了
之外没有额外的功能Constant
属性的记录行为。

In MATLAB, classes can define Constant properties, but not "static" properties in the sense of other languages like C++. There were beta releases that experimented with "Static" properties and the undocumented attribute remains from then. However, the Static attribute is undocumented, should not be used, and will likely be removed in a future MATLAB release. R2008a implements it as a synonym for Constant and provides no additional functionality beyond the documented behavior of Constant properties.

常量属性不能从
中指定的初始值更改
属性声明。有一个
的几个原因,为什么MATLAB工作
的方式。首先,MATLAB具有
长期规则,变量
总是优先于函数和类的名称
,而
赋值语句引入
变量已经存在。
因此,形式为AB
= C的任何表达式都将引入一个新的变量A,它是一个包含
字段B的结构数组,其值为C.如果AB = C
可以引用
类A的静态属性,那么A类将取代变量A的
,这个
将是非常重要的
与以前的版本不兼容
of MATLAB。这意味着包含赋值
语句AB = C的
m文件可以使其
含义通过引入
改变,名为A的某个地方b $ b MATLAB路径。 MATLAB程序员有
总是能够依赖赋值
语句引入变量
影响任何其他使用相同的名称。

Constant properties may not be changed from the initial value specified in the property declaration. There are a couple of reasons why MATLAB works the way it does. First, MATLAB has longstanding rules that variables always take precedent over the names of functions and classes and that assignment statements introduce a variable if one doesn't already exist. Thus, any expression of the form "A.B = C" will introduce a new variable A that is a struct array containing a field B whose value is C. If "A.B = C" could refer to a static property of class A, then class A would take precedent over variable A and this would be a very significant incompatibility with prior releases of MATLAB. It would mean that an m-file containing the assignment statement "A.B = C" could have its meaning changed by the introduction of a class named A somewhere on the MATLAB path. MATLAB programmers have always been able to rely on assignment statements introducing variables that shadow any other use of the same name.

第二,我们观察到静态
数据很少用于其他类
中,除了作为
类中的私有数据或作为公共常量。对于
示例,对多个Java
类库的调查发现,所有public
静态字段也是final。在
MATLAB中,常量属性可以是
,就像public final static
在Java中使用字段。对于
类内部的数据,MATLAB已经具有持久的
变量,可以在私人或受保护方法的
内部创建,或

类私有使用的局部函数。还有很好的理由,
避免在MATLAB中的静态数据,其中
可能。如果一个类有静态数据
,可能很难在多个应用程序
中使用相同的
类,因为静态数据可以是

之间的冲突源应用程序。在一些其他语言中,
这不是一个问题,因为
不同的应用程序单独
编译为在
中运行的可执行文件不同的进程与不同的
类静态数据的副本。在
MATLAB中,通常许多不同的
应用程序可以在
相同的进程和环境中运行,每个类具有
单个副本。

Second, we have observed that static data is rarely used in other classes except as private data within the class or as public constants. For example, a survey of several Java class libraries found that all public static fields were also final. In MATLAB, Constant properties can be used like "public final static" fields in Java. For data internal to a class, MATLAB already has persistent variables that can be created inside of private or protected methods or local functions privately used by a class. There are also good reasons to avoid static data in MATLAB where possible. If a class has static data, it can be difficult to use the same class in multiple applications because the static data can be a source of conflicts among applications. In some other languages, this is less of an issue because different applications are separately compiled into executables running in different processes with different copies of class static data. In MATLAB, frequently many different applications may be running in the same process and environment with a single copy of each class.

这篇关于如何获取MATLAB类中的静态成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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