Trouble with inheritance i MATLAB OOP
3 ビュー (過去 30 日間)
古いコメントを表示
Govind Sankar Madhavan Pillai Ramachandran Nair
2024 年 7 月 8 日
編集済み: Govind Sankar Madhavan Pillai Ramachandran Nair
2024 年 7 月 8 日
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.
0 件のコメント
採用された回答
Matt J
2024 年 7 月 8 日
編集済み: Matt J
2024 年 7 月 8 日
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 件のコメント
Govind Sankar Madhavan Pillai Ramachandran Nair
2024 年 7 月 8 日
編集済み: Govind Sankar Madhavan Pillai Ramachandran Nair
2024 年 7 月 8 日
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!