Spec-Zone.ru › Octave 9

34.6.5 Классы-значения и классы-ссылки

Существуют два принципиально разных типа классов classdef, главное различие между которыми заключается в поведении при присваивании переменных. Первый тип — это классы-значения:

classdef value_class
  properties
    prop1
  endproperties

  methods
    function obj = set_prop1 (obj, val)
      obj.prop1 = val;
    endfunction
  endmethods
endclassdef

Присваивание объекта этого класса другой переменной по сути создаёт новый объект:

>> a = value_class ();
>> a.prop1 = 1;
>> b = a;
>> b.prop1 = 2;
>> b.prop1
⇒ ans =  2
>> a.prop1
⇒ ans =  1

Но это также означает, что, возможно, потребуется вручную присвоить объекту результат метода, изменяющего свойства:

>> a = value_class ();
>> a.prop1 = 1;
>> a.set_prop1 (3);
⇒ ans =

<object value_class>

>> ans.prop1
⇒ ans =  3
>> a.prop1
⇒ ans =  1

Второй тип — это классы-ссылки. Такие классы должны быть производными от абстрактного класса handle:

classdef handle_class < handle
  properties
    prop1
  endproperties

  methods
    function set_prop1 (obj, val)
      obj.prop1 = val;
    endfunction
  endmethods
endclassdef

В следующем примере переменные a и b ссылаются на один и тот же объект класса handle_class:

>> a = handle_class ();
>> a.prop1 = 1;
>> b = a;
>> b.prop1 = 2;
>> b.prop1
⇒ ans =  2
>> a.prop1
⇒ ans =  2

Свойства объектов, изменённые методом класса-ссылки, сохраняют изменения:

>> a.set_prop1 (3);
>> a.prop1
⇒ ans =  3

© 1996–2026 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v9.4.0/Value-Classes-vs_002e-Handle-Classes.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API