gpt4 book ai didi

matlab - Matlab中的继承多态性

转载 作者:太空宇宙 更新时间:2023-11-03 19:56:37 25 4
gpt4 key购买 nike

我尝试阅读 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.

同样的概念也适用于 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

creation of instance of class A:

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

执行上面这行代码,你会看到:

Object of class A

creation of instance of class B:

b = B () ;
b.printSomething()

执行上面这行代码,你会看到:

Object of class B

Type checking:

isa (b,'A')

1

isa (b,'B')

1

关于matlab - Matlab中的继承多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com