Effacer les filtres
Effacer les filtres

Trouble with inheritance i MATLAB OOP

23 vues (au cours des 30 derniers jours)
Govind Sankar Madhavan Pillai Ramachandran Nair
I have two classes. ParameterFile and PathParameterFile. Both are used to read from an xml file and get its contents. PathParameterFile is a child class of ParameterFile. In PathParameterFile constructor, you pass in a path of the xmlfile, it reads out the xmlfile and stores it in a struct. This one has 2 Properties.
properties
Path;
xmlStruct;
end
And the constructor is like this
function obj = PathParameterFile(path)
obj.xmlStruct = readstruct(path);
end
Then there is a second function called extractdata that extracts individual data from the struct and stores it in seperate properties. Here in this xml file we only have one value, that is the path to a different xml file.
function extractData(obj)
obj.Path = obj.xmlStruct.Path;
end
This is the PathParameterFile class which is also the child class.
Now the Parent Class which is ParameterFile is similar, but it has more Properties and methods to return the properties. These are the properties
properties
Path;
MaximumVoltage;
MinimumVoltage;
AllowedDeviation;
xmlStruct;
end
And this is constructor and extractData function.
function obj = ParameterFile(path)
%xmlFile = 'X:\5000 R&D\BM57V311_FAT_DataAnalysis\FAT Upgrade\Fat_Upgrade.xml';
obj.xmlStruct = readstruct(path);
end
function extractData(obj)
batteryType = 'battery1';
obj.Path = obj.xmlStruct.(batteryType).Path;
end
Now as i said there are functions to return the properties. That is here I get to return the Path. So I have a function like this.
function outputPath = getPath(obj)
outputPath = obj.Path;
end
This is my idea. So I enter the path in the command window and store it in path1 and use that path to create an object of PathParameterFile, like this
pm1 = PathParameterFile(path1);
Then I use the function getPath to retrieve the path in xml file and store it in path2.
path2 = pm1.getPath;
And use path2 to create an object of ParameterFile and then get all the values there. So getPath is only writtern in Parent Class, its not written in Child Class. I just plan to call it using the child Class . This is the total idea. But my proplem is when I create an object of PathParameterFile i.e
pm1 = PathParameterFile(path1)
, its giving an error
Cannot define property 'Path' in class
'PathParameterFile' because the property has already
been defined in the superclass 'ParameterFile'.
So what is the problem here. I need to use Path in both child and parent class, so that I can write the getPath function only in ParentClass and call it using the child class. Is that not possible. What needs to be done. Thank you.

Réponse acceptée

Matt J
Matt J le 8 Juil 2024 à 10:50
Modifié(e) : Matt J le 8 Juil 2024 à 10:51
Remove these property declarations from PathParameterFile,
properties
Path;
xmlStruct;
end
Since these properties are already defined in the parent class, they are already available in the child class and you do not need to define them again there (and it is forbidden to do so).
  1 commentaire
Govind Sankar Madhavan Pillai Ramachandran Nair
Modifié(e) : Govind Sankar Madhavan Pillai Ramachandran Nair le 8 Juil 2024 à 12:54
Thank you and it worked. Now I enter into the world of inheritance in OOP in MATLAB. This was the first time I used it and now since it works, I have an idea. Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by