34.4.2运算符重载

下表显示,对于每个内置的数字运算,在为auser类提供重载方法时要使用的相应函数名。

活动 方法 描述
a + b plus (a, b) Binary addition
a - b minus (a, b) Binary subtraction
+a uplus (a) Unary addition
-a uminus (a) Unary subtraction
a .* b times (a, b) Element-wise multiplication
a * b mtimes (a, b) Matrix multiplication
a ./ b rdivide (a, b) Element-wise right division
a / b mrdivide (a, b) Matrix right division
a .\ b ldivide (a, b) Element-wise left division
a \ b mldivide (a, b) Matrix left division
a .^ b power (a, b) Element-wise power
a ^ b mpower (a, b) Matrix power
a < b lt (a, b) Less than
a <= b le (a, b) Less than or equal to
a > b gt (a, b) Greater than
a >= b ge (a, b) Greater than or equal to
a == b eq (a, b) Equal to
a != b ne (a, b) Not equal to
a & b and (a, b) Logical and
a | b or (a, b) Logical or
!a not (a) Logical not
a' ctranspose (a) Complex conjugate transpose
a.' transpose (a) Transpose
a:b colon (a, b) Two element range
a:b:c colon (a, b, c) Three element range
[a, b] horzcat (a, b) Horizontal concatenation
[a; b] vertcat (a, b) Vertical concatenation
a(s_1,…,s_n) subsref (a, s) Subscripted reference
a(s_1,…,s_n) = b subsasgn (a, s, b) Subscripted assignment
b(a) subsindex (a) Convert object to index
disp disp (a) Object display

表34.1:可用的重载运算符及其相应的类方法

一个例子mtimes多项式类的方法可能看起来像

function p = mtimes (a, b)
  p = polynomial (conv (double (a), double (b)));
endfunction

版权所有 © 2024-2025 Octave中文网

ICP备案/许可证号:黑ICP备2024030411号-2