Spec-Zone.ru › Octave 9

34.6.1 Создание класса classdef

Очень простой класс-значение classdef (см. Классы-значения и классы-ссылки) определяется следующим образом:

classdef some_class
  properties
  endproperties

  methods
  endmethods
endclassdef

В отличие от классов старого типа, блок properties-endproperties, а также блок methods-endmethods можно использовать для определения свойств и методов класса. Поскольку оба блока пусты, в данном случае их можно опустить.

Для простоты более сложная реализация класса classdef снова показана на примере polynomial (см. Создание класса):

classdef polynomial2
  properties
    poly = 0;
  endproperties

  methods
    function p = polynomial2 (a)
      if (nargin == 1)
        if (isa (a, "polynomial2"))
          p.poly = a.poly;
        elseif (isreal (a) && isvector (a))
          p.poly = a(:).';  # force row vector
        else
          error ("polynomial2: A must be a real vector");
        endif
      endif
    endfunction

    function disp (p)
      a = p.poly;
      first = true;
      for i = 1 : length (a);
        if (a(i) != 0)
          if (first)
            first = false;
          elseif (a(i) > 0 || isnan (a(i)))
            printf (" +");
          endif
          if (a(i) < 0)
            printf (" -");
          endif
          if (i == 1)
            printf (" %.5g", abs (a(i)));
          elseif (abs (a(i)) != 1)
            printf (" %.5g *", abs (a(i)));
          endif
          if (i > 1)
            printf (" X");
          endif
          if (i > 2)
            printf (" ^ %d", i - 1);
          endif
        endif
      endfor

      if (first)
        printf (" 0");
      endif
      printf ("\n");
    endfunction
  endmethods
endclassdef

Объект класса polynomial2 создаётся вызовом конструктора класса:

>> p = polynomial2 ([1, 0, 1])
⇒ p =

 1 + X ^ 2

© 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/Creating-a-classdef-Class.html

Spec-Zone.ru

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