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

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

问题描述

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

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

这不起作用:

classdef A

    properties ( Static )
        m = 0;
    end
end

建议使用关键字Constant"代替Static",常量属性不能修改.我想要一个对 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?

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

在这种情况下,您应该从基类继承所有类,如下所示.

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

推荐答案

你不能,这是设计使然.您应该使用 persistent 变量(来自 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)!

为了完整起见,我应该提到实际上截至 2010b 有一个未记录且可能不再受支持的 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<的答案/a>,MATLAB OO 组经理:

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

在 MATLAB 中,类可以定义常量属性,但不是静态"其他意义上的属性C++ 等语言.有测试版试验过的版本静态"属性和未记录的属性仍然来自然后.但是,静态属性是未记录,不应使用,以及将来可能会被删除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有长期存在的变量规则始终优先于名称函数和类以及赋值语句引入了一个如果一个变量不存在.因此,任何形式为A.B= C" 将引入一个新变量 A,它是一个包含值为 C 的字段 B.如果A.B = C"可以指的是一个静态属性A类,然后A类将采取变量 A 的先例和这个将是一个非常重要的与先前版本不兼容MATLAB 的.这意味着一个包含作业的 m 文件语句A.B = C"可以有它的意义因介绍而改变某处名为 A 的类的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 的调查类库发现所有公共静态字段也是最终的.在MATLAB,常量属性可以是像公共最终静态"一样使用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天全站免登陆