matlab - 如何获取MATLAB类中的静态成员变量?

标签 matlab class oop static-functions matlab-class

有没有办法在 MATLAB 类中定义静态成员变量?

这行不通:

classdef A

    properties ( Static )
        m = 0;
    end
end

建议使用关键字“Constant”而不是“Static”,常量属性不能被修改。我想要一个对 A 类的所有对象通用的变量,并且我希望能够在 A 类的方法中修改该变量。

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


发现可以通过在静态成员函数中使用持久变量来解决问题。

在这种情况下,您应该像下面这样从基类继承所有类。

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

最佳答案

你不能,这是设计使然。您应该使用 persistent 变量(在 2011 年应用了 1980 年的 MATLAB 技术)!

为了完整起见,我应该提到实际上截至 2010b 有一个未记录且可能不再受支持的 static 属性修饰符。

背景见here Dave Foti的答案, MATLAB OO组长:

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.

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.

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 - 如何获取MATLAB类中的静态成员变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6450204/

相关文章:

algorithm - 正方形内两条线之间的区域 [-1,+1] x [-1,+1]

image - 平均损坏图像以消除 MATLAB 中的噪声的问题

Java:如何在另一个类中使用一个类中实例化的对象?

design-patterns - 如何设计带有非面向对象部分的 UML 类图?

matlab - 如何结束 MATLAB/Octave 函数定义?

c++ - 在C++中使用imfilter(matlab)

powershell - 如何在 PowerShell 中创建私有(private)类成员?

c++ - 如果我在整个类上使用 std::swap,是否会使用专门的 shared_ptr::swap() 函数?

javascript - 检查对象是否立即继承自构造函数

javascript - 如何将两个数组值连接成 JS 对象的同级键/值对