Inheritance Polymorphism in Matlab

I tried to read documents in the Matlab language for a while, and either it does not exist, or they called it something that did not happen to me.

In major languages ​​such as Java, for example, I can implement a strategy template with simple polymorphism as follows:

class A{
    void foo(){
        System.out.println("A");
    }
}

class B : A{
    void foo(){
        System.out.println("B");
    }
}

A ab = new B();
ab.foo();//prints B, although static type is A.

the same concept is also available in interpreter languages ​​such as python.

Is there something similar in Matlab (I use 2016a).

How it's called? what is the syntax?

+4
source share
1 answer
classdef A < handle
    %UNTITLED Summary of this class goes here
    %   Detailed explanation goes here

    methods
         function obj =  A ()            
         end         
        function printSomething (obj)
            disp ('Object of class A') ;
        end        
    end    
end


classdef B < A
    %UNTITLED2 Summary of this class goes here
    %   Detailed explanation goes here

    methods
        function obj =  B ()
        end
        function printSomething (obj)
            disp ('Object of class B');
        end
    end    
end

instantiation of class A:

a = A () ;
a.printSomething ();

By executing the above line of code, you will see:

Object of class A

instantiation of class B:

b = B () ;
b.printSomething()

By executing the above line of code, you will see:

Object of class B

Type Check:

isa (b,'A')

1

isa (b,'B')

1
+5
source

Source: https://habr.com/ru/post/1653299/


All Articles