Python to Matlab property decorator function in class

17 次查看(过去 30 天)
I have this piece of code in python that i want to translate to matlab. Any ideas how it could be done?
class Celsius:
def __init__(self, temperature = 0):
self._temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
@property
def temperature(self):
print("Getting value")
return self._temperature
@temperature.setter
def temperature(self, value):
self._temperature = value

回答(1 个)

Thibaut Jacqmin
Thibaut Jacqmin 2022-1-28
classdef Celsius < handle
properties
temperature
end
methods
function obj = Celsius(temperature)
arguments
temperature double = 0
end
obj.temperature = temperature;
end
function y = to_fahrenheit(obj)
y = (obj.temperature * 1.8) + 32;
end
function y = get.temperature(obj)
disp("Getting value")
y = obj.temperature;
end
function set.temperature(obj, value)
disp("Setting value")
obj.temperature = value;
end
end
end

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by