Package
/test.m, test function
/test2.m, test function
/+Package/@Car/Car.m, class
/+Package/@Car/disp.m, method of class in a different file
/+Package/@Car/disp2.m, method of class in a different file
/+Package/Num.m, class
/+Package/disp.m, function
- +Package, package folder, is a namespace within which names must be unique
- @Car, class folder, within which only one class is defined, the methods can be saved in the different file in the class folder
Car
classdef Car
properties (Access = private)
producer
end
properties (Access = protected)
year = 0
end
properties (Access = public)
model
end
methods
% Constructor
function self = Car(p, m)
self.producer = p;
self.model = m;
end
% Accessor
function p = getProducer(self)
p = self.producer;
end
function y = getYear(self)
y = self.year;
end
function m = getModel(self)
m = self.model;
end
disp(self)
disp2(self)
% Mutator
function self = setProducer(self, p)
self.producer = p;
end
function self = setYear(self, y)
self.year = y;
end
function self = setModel(self, m)
self.model = m;
end
end
end
function disp(self)
fprintf('Producer: %s\nModel: %s\nYear: %d\n', self.producer, self.model, self.year);
end
function disp2(self)
fprintf('------------------------\n');
disp(self);
fprintf('------------------------\n');
end
Num
classdef Num
properties
value
end
methods
% Constructor
function self = Num(n)
self.value = n;
end
% Accessor
function disp(self)
fprintf('Num: %d\n', self.value);
end
% Mutator
function self = add(self, v)
self.value = self.value + v;
end
function self = multiply(self, v)
self.value = self.value*v;
end
end
end
Other Objects
function disp(c)
c.disp();
end
Test
function test()
% Create a Car Object
c = Package.Car('Honda', 'Accord');
c.disp();
c.disp2();
% Create a Num Object
n = Package.Num(80);
n.disp();
% Call Functions in Package
Package.disp(c);
end
function test2()
import Package.*
% Create a Car Object
c = Car('Honda', 'Accord');
c.disp();
c.disp2();
% Create a Num Object
n = Num(80);
n.disp();
% Call Functions in Package
disp(c);
end