Gul*_*zar 4 oop polymorphism matlab
我试图阅读matlab文档一段时间,要么这不存在,要么将它命名为我没有想到的东西.
例如,在Java等主流语言中,我可以使用简单的多态来实现策略模式,如下所示:
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.
Run Code Online (Sandbox Code Playgroud)
同样的概念也可用于解释器语言,如python.
在Matlab中是否存在这样的东西(我正在使用2016a).
这叫什么?语法是什么?
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
Run Code Online (Sandbox Code Playgroud)
创建A类实例:
a = A () ;
a.printSomething ();
Run Code Online (Sandbox Code Playgroud)
通过执行上面的代码行,您将看到:
Object of class A
Run Code Online (Sandbox Code Playgroud)
创建B类实例:
b = B () ;
b.printSomething()
Run Code Online (Sandbox Code Playgroud)
通过执行上面的代码行,您将看到:
Object of class B
Run Code Online (Sandbox Code Playgroud)
类型检查:
isa (b,'A')
1
isa (b,'B')
1
Run Code Online (Sandbox Code Playgroud)