MATLAB Object-Oriented Programming

(Joyce) #1

Rather than copying the code used to implement the dlnode class, and then expanding
upon it, you can derive a new class from dlnode (that is, subclass dlnode). You can
create a class that has all the features of dlnode and also defines its own additional
features. And because dlnode is a handle class, this new class is a handle class too.


NamedNode Class Definition


To use the class, create a folder named @NamedNode and save NamedNode.m to this
folder. The parent folder of @NamedNode must be on the MATLAB path. Alternatively, save
NamedNode.m to a path folder.


The following class definition shows how to derive the NamedNode class from the dlnode
class:


classdef NamedNode < dlnode
properties
Name = ''
end
methods
function n = NamedNode (name,data)
if nargin == 0
name = '';
data = [];
end
n = n@dlnode(data);
n.Name = name;
end
end
end


The NamedNode class adds a Name property to store the node name.


The constructor calls the class constructor for the dlnode class, and then assigns a value
to the Name property.


Use NamedNode to Create a Doubly Linked List


Use the NamedNode class like the dlnode class, except that you specify a name for each
node object. For example:


n(1) = NamedNode('First Node',100);
n(2) = NamedNode('Second Node',200);
n(3) = NamedNode('Third Node',300);


Implementing Linked Lists with Classes
Free download pdf