【发布时间】:2021-01-09 04:22:48
【问题描述】:
我在下面有这个 Python 代码。下面是我尝试将其转换为 MATLAB 代码。我是classdef 的新手,我正在进行转换以供练习。 counter 的问题(想计算我调用构造函数的次数),我希望得到有关如何更正我的转换的反馈。
Python 版本:
class Test:
counter = 0
def __init__(self, id):
self.id = id
Test.counter += 1
我的 MATLAB 转换尝试:
classdef Test
properties
counter=0;
id;
end
methods
function self= Test(id)
self.id = id;
self.counter = self.counter+1;
end
end
end
【问题讨论】:
-
构造函数总是为每个对象调用一次。 Python 代码中的
counter是否真的有一个不是 1 的值?
标签: matlab class oop constructor