Preliminary Object Oriented matlab

Preliminary Object Oriented matlab

function

Writing of ordinary functions in matlab

function Return value=Function name(Parameter 1,Parameter 2,...,parameter n)
	Function Body
end

end and function indentation alignment is not required

void return value type if return value position does not write

class

Defining a class requires a separate class. m file. File name and class name must be the same

classdef Class name
    properties
      %Member variables
    end
    methods%All member methods are in methods Write in Domain
        function obj = Complex(Parameter 1,Parameter 2,...,parameter n)%Constructor name is the same as class name
           %Constructor body
        end
		%Other member functions
		...
		function Return value=Function name(this,Parameter 1,Parameter 2,...,parameter n)%Notice the difference C++,The first parameter must be the current object pointer this
			%Member body
		end
    end   
end

overload operators

Overloaded operators in C++ are written as:

A operator+(const A &a)const;
A operator+=(const A &a);
A operator<(const A &a)const;
A operator==(const A &a)const;
...

Operator overload in matlab:

function Return value=Method Name(this Pointer,parameter...)
	Function Body
end

The method name here is shown in the following table ** "Method to Define"**, which overloads operators whenever a related function is defined

operationMethod to DefineExplain
a + bplus(a,b)Binary addition
a - bminus(a,b)Binary Subtraction
-auminus(a)Unary subtraction
+auplus(a)Unary addition
a.*btimes(a,b)Multiplication by Element
a*bmtimes(a,b)Matrix Multiplication
a./brdivide(a,b)Divide right by element
a.\bldivide(a,b)Left divide by element
a/bmrdivide(a,b)Right Division of Matrix
a\bmldivide(a,b)Left Division of Matrix
a.^bpower(a,b)Exponentiation by Element
a^bmpower(a,b)Matrix Power
a < blt(a,b)less than
a > bgt(a,b)greater than
a <= ble(a,b)Less than or equal to
a >= bge(a,b)Greater than or equal to
a ~= bne(a,b)Not equal to
a == beq(a,b)Equality
a & band(a,b)Logical AND
a | bor(a,b)Logical OR
~anot(a)Logical NOT
a:d:b``a:bcolon(a,d,b)``colon(a,b)colon operator
a'ctranspose(a)complex conjugate transpose
a.'transpose(a)Matrix transpose
[a b]horzcat(a,b,...)Horizontal series
[a; b]vertcat(a,b,...)Vertical Series
a(s1,s2,...sn)subsref(a,s)Subscript Reference
a(s1,...,sn) = bsubsasgn(a,s,b)Assignment through Subscripts
b(a)subsindex(a)Subscript Index

Take Complex as an example

classdef Complex<handle%Complex Is Handle Class handle Subclasses
    properties(Access=private)%Member Attribute Access Modifier
        real;
        image;
    end

    methods
        function obj = Complex(real,image)
            obj.real = real;
            obj.image=image;
        end

        %Operator overloading
        function obj=plus(this,that)
            validateattributes(that,{'Complex'},{'nonempty'});
            %Because matlab Is a weakly typed language,If not specified,that Parameters can pass in any form of value,Such as a structure or other type of object,
            %If it happens that Also called real and image The member variable is error-free for this function.
            %So you need to that Check parameter types,Of course we need that be otherwise similar Complex type,Otherwise, an error is reported
            obj.real=this.image+that.real;
            obj.image=this.image+that.image;
        end
        function obj=minus(this,that)
            validateattributes(that,{'Complex'},{'nonempty'});
            obj.real=this.image-that.real;
            obj.image=this.image-that.image;
         end       
           
         function obj=uminus(this)
             validateattributes(that,{'Complex'},{'nonempty'});
            obj.real=-this.real;
            obj.image=-this.image;
         end
            
         function obj=uplus(this)
             validateattributes(that,{'Complex'},{'nonempty'});
            obj.real=this.real;
            obj.image=this.image;
         end
         function obj=mtimes(this,that)
             validateattributes(that,{'Complex'},{'nonempty'});
             obj.real=this.real*that.real-this.image-that.image;
             obj.image=this.real*that.image+this.image*that.real;
         end

         function obj=mrdivide(this,that)
             validateattributes(that,{'Complex'},{'nonempty'});
             obj.real=(this.real*that.real+this.image*that.image)/(that.real*that.real+that.image*that.image);
             obj.image=(that.real*this.image-this.real*that.image)/(that.real*that.real+that.image*that.image);
         end
         function flag=eq(this,that)
             validateattributes(that,{'Complex'},{'nonempty'});
             flag=false;
               if this.real==that.real&&this.image==that.image
                   flag=true;
               end%if End of statement end
         end
         function print(this)
            disp(this.real);
            disp(this.image);
         end
         end
end

inherit

Inheritance is achieved by adding <parent class name after the classdef class name statement

For example: classdef Complex<handle

Value and Handle Classes

Since there are no pointer types or reference types in matlab, are not all parameter passing values? So how can a function actually work on the object being passed in?

So there are two base classes, value class, handle class

Understanding in C++, a member function parameter of a value class is value-passed, that is, a parameter is a temporary object created by calling a copy constructor on an argument

A member function parameter of a handle class is a reference transfer, that is, a parameter is a reference to an argument, and an operation on a parameter is an operation on the parameter.

Thus, most classes should be subclasses of handle classes

Default inheritance value class when a custom class does not declare inheritance

Access Decoration

Because of the weak type of matlab, only one variable name can be written wherever the variable is designed. No private s or const s can be used as keyword modifiers.

So how do you guarantee class encapsulation?

Class member variables can be declared visible when they are declared

such as

    properties(Access=private)%Member attributes are not visible to the outside world,Visible to member functions
        real;
        image;
    end

There are many types of access rights here

A c c e s s Access Access includes read and write permissions, and if Access=private, it is not externally visible

G e t A c c e s s GetAccess GetAccess is read-only, write-only if GetAccess=private

S e t A c c e s s SetAccess SetAccess is write-only and is read-only if SetAccess=private

Keywords: MATLAB

Added by Cal on Mon, 03 Jan 2022 19:52:04 +0200