subclass properties to superclass
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi guys,
I would like to make subclass properties as properties of superclass.
For example i have a tree structure, in class clcore i have a subclass clmaterialcore and then subsubclasses of clmaterialcore is clsteel, clferrite. I need to decide which type of material to be used.
This is given as a property "matcore". matcore is one of the properties of clcore. i did in a way, but i dont know whether iam doing it correct or wrong. i will explain you with an example since it is a small part of my project.
classdef clcore
properties
Matcore
mc
end
end
This is my superclass
classdef clmaterialcore
properties
mat
m
end
end
This is a subclass clmaterialcore
classdef clsteel
properties
sp
S
end
end
This is a subsubclass steel
classdef clferrite
properties
fp
F
end
end
This is subsubclass ferrite.
Generally the properties sp, fp are the functions which forms a struct . sp forms astruct using the function steel_catalog, fp forms a struct using the function ferrite_catal og.Iam not able to understand how to implement this. Should i implement it in main file or should i implement within the class.
%calculates the values of the clSisteel
objIS.sp=steel_catalog(objIS.S);
%calculates the values of the clFerrite
objIF.fp=ferrite_catalog(objIF.F);
%chooses which maaterial to be used
if objImat.mat==1
objImat.m=steel_catalog(objIS.S);
elseif objImat.mat==2
objImat.m=ferrite_catalog(objIF.F);
else
disp('No such material catalog for core');
return
end
%calculates the values of core
objICore.Matcore=objImat.mat;
objICore.mc=objImat.m;
This is my main file.
Thankyou,
Mohana
댓글 수: 0
답변 (1개)
Steven Lord
2020년 7월 22일
As written you don't have a class hierarchy. You have four completely independent classes. If you want one of your classes to be a subclass of a different one of your classes, you need to have the subclass inherit from the superclass.
It's not really clear to me what you're asking. Are you saying that you want your clsteel class to take whatever the user passes in to construct it and call a function steel_catalog with some of that data to initialize the properties of the object? You can do that in the clsteel class constructor.
Let's take a step back. Can you describe in words not code the system you're trying to model? Are you trying to build a car and specify the material from which each part is made, for example?
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!