Matlab中的继承多态性 [英] Inheritance Polymorphism In Matlab

查看:96
本文介绍了Matlab中的继承多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图阅读matlab文档有一段时间了,要么这不存在,要么他们给它起了一些我不曾想到的东西.

I was trying to read the matlab docs for a while, and either this doesn't exist or they named it something that did not occur to me.

例如,在Java等主流语言中,我可以实现具有简单多态性的Strategy模式,如下所示:

In mainstream languages such as Java for example, I can implement the Strategy pattern with simple polymorphism like so:

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.

在诸如python之类的解释器语言中也可以使用相同的概念.

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

在Matlab中是否有类似的东西(我正在使用2016a).

Is there something like this in Matlab (I'm using 2016a).

它叫什么?语法是什么?

What is it called? what is the syntax?

推荐答案

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

创建类A的实例:

creation of instance of class A:

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

通过执行以上代码行,您将看到:

By executing above line of code, you will see:

Object of class A

创建类B的实例:

creation of instance of class B:

b = B () ;
b.printSomething()

通过执行以上代码行,您将看到:

By executing above line of code, you will see:

Object of class B

类型检查:

Type checking:

isa (b,'A')

1

isa (b,'B')

1

这篇关于Matlab中的继承多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆