Hi guys,
I would like to pass my propert values of super class to subclass? I could transfer the properties but not the values of it. I am sending you the code that i made. Please tell me how could i correct it.
classdef clWinding
properties
Npcs
Npcp
end
properties (Dependent)
kb
end
methods
function objW = clWinding (Npcs, Npcp)
objW.Npcs = Npcs;
objW.Npcp = Npcp;
end
function kb = get.kb (objW)
kb = objW.Npcs + objW.Npcp;
end
end
end
This is the superclass
classdef clLitz <clWinding
properties
Litz
end
methods
function objLitz = clLitz (Npcs, Npcp, Litz)
objLitz @ clWindingPrimary (Npcs, Npcp);
objLitz.Litz = Litz;
end
end
end
When i did this i could pass the fields but not values of it. Can anyone tell me what mistake i have done and help me to resolve it.
Thnak you

4 件のコメント

Steven Lord
Steven Lord 2020 年 7 月 27 日
As you've written it clWinding is your superclass and clLitz the subclass. The subclass inherits from the superclass.
Can you show the exact code you run to try to create (instantiate) a clLitz object and show the full and exact text of any error messages you received when you executed that code?
kanuri venkata mohana
kanuri venkata mohana 2020 年 7 月 27 日
Hi Steven,
Thankyou for your quick reply. It is a small part of my code. I cannot run a single class. So i think i cannot send the errors. When i created an object "objLitz" for clLitz, i got all the parameters which i defined in clWindingPrimary but i didnot get the values of them. how to do that was my question?
Steven Lord
Steven Lord 2020 年 7 月 27 日
Because your objLitz isa clWinding object (through inheritance) you can ask for the values of the (for example) Npcs property it has because it is a clWinding object.
obj = clLitz(1, 2, 3) % using dummy values
N = obj.Npcs
kanuri venkata mohana
kanuri venkata mohana 2020 年 7 月 27 日
Once again Thankyou for your reply. So, you mean i need to assign values manually for the superclass properties in my subclass right.

サインインしてコメントする。

 採用された回答

per isakson
per isakson 2020 年 7 月 30 日

0 投票

There are syntax errors in your code. Try to keep the little box in the upper right corner, , green at all time.
I've fixed your code, based on a bit of guessing, and run
>> objLitz = clLitz( -17, 17, 991 );
>> objLitz
objLitz =
clLitz with properties:
Litz: 991
Npcs: -17
Npcp: 17
kb: 0
where
classdef clWinding
properties
Npcs
Npcp
end
properties (Dependent)
kb
end
methods
function objW = clWinding( Npcs, Npcp )
objW.Npcs = Npcs;
objW.Npcp = Npcp;
end
function kb = get.kb( objW )
kb = objW.Npcs + objW.Npcp;
end
end
end
and
classdef clLitz < clWinding
properties
Litz
end
methods
function objLitz = clLitz (Npcs, Npcp, Litz)
% How to Call Superclass Constructor
objLitz = objLitz@clWinding( Npcs, Npcp);
objLitz.Litz = Litz;
end
end
end

その他の回答 (1 件)

Matt J
Matt J 2020 年 7 月 28 日
編集済み: Matt J 2020 年 7 月 28 日

0 投票

Your terminolgy is confusing, so I'm not completely sure I understand your question. "Classes" do not have property values. Only "objects" of classes have property values. If you have an object obj_super and you want to copy all of its non-Dependent property values to a different object obj_sub, then you can use the attached file,
obj_sub=copyprops(obj_super,obj_sub)

カテゴリ

ヘルプ センター および 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!

Translated by