objects of one class into another class
53 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
i want to use objects of one class into anothe class. I tried to use it as a variable but it seems that it is not working. Could anyone help me with this problem?
classdef clwinding
properties
d
w
end
methods
function objw = clwinding (d, w)
objw.d = d;
objw.w = w;
end
end
end
I want to use thsi object in another class. Is it posssible without using interface.
classdef clloss
properties
l
end
properties (Dependent)
s
end
methods
function objl = clloss (l)
objl = objl.l;
end
function s = get.s (objl)
s = objl.l + objw.d;
end
end
end
0 Kommentare
Antworten (3)
Stijn Haenen
am 30 Jul. 2020
If you define for example your class like this, you can multiply two object values:
classdef BasicClass
properties
Value {mustBeNumeric}
end
methods
function r = multiplyBy(obj,n)
r = [obj.Value] * n;
end
end
end
a = BasicClass;
a.Value=10;
ans1=multiplyBy(a,3);
b=BasicClass;
b.Value=5;
ans2=multiplyBy(a,b.Value);
Steven Lord
am 30 Jul. 2020
Your clwinding class is syntactically valid.
Your clloss class, on the other hand, has some conceptual errors. I'm going to make one change, replacing the lower-case L property and variable with upper-case L (just personal preference, I avoid using lower-case l because of the ease with which it can be confused with the number 1.)
classdef clloss
properties
L
end
properties (Dependent)
s
end
methods
function objL = clloss (L)
objL = objL.L;
end
I see three problems here.
The first and most important is that the constructor of a class must return an instance of that class. So objL must be a clloss object. Is it?
The second problem is that when you run this line of code, nothing has assigned a value to objL.L yet so it is a 0-by-0 double array. So the answer to the question I asked in the first problem, "Is it?", is no. As written this constructor returns the 0-by-0 double and MATLAB will error because that is not a clloss object.
The third problem is that you don't use the value the user of the class passed into the constructor. Your mental model may be that because the input argument has the same name as the property that it's automatically assigned into that property; if so that is incorrect.
If your goal is to assign the value the user of the class passed into the constructor into the property then return the object, your constructor should be:
function objL = clloss(L)
objL.L = L;
end
Now let's look at the property get method for the Dependent property s.
function s = get.s (objL)
s = objL.L + objw.d;
end
The first part of this is fine. objL will be a clloss object and it has a property L. The second part is not fine. Nowhere in this code do you define objw.
Because of the fact that the definition of the constructor of the clwinding class calls its output argument objw I think you're thinking that this will retrieve "the" value of the d property of clwinding and use it here. But as you've written the clwinding class, each instance of that class can have a different value for the d property. So which clwinding object's d property do you want to use here? Are you guaranteed that one and only one clwinding object exists? Can you create a clloss object and try to access its s property when no clwinding objects exist? Can you try to get the s property when twenty clwinding objects exist?
If d is some sort of constant that has the same value for all clwinding objects, this code can be repaired. Make the d property of the clwinding class a Constant property. Then you can refer to that constant value using the name of the class, regardless of how many (0, 1, 20, etc.) instances of the class exist.
properties(Constant)
d = 42;
end
Use:
q = clwinding.d;
If each clwinding can have a different value for d, you're probably going to need to store the specific clwinding for this clloss object in a property of the clloss object and reference the d property of that clwinding object. Or you're going to need to design some other way to get "the" clwinding for this particular clloss object.
2 Kommentare
Steven Lord
am 30 Jul. 2020
You're conflating two different but related things.
The properties blocks in a classdef file says what properties an instance of that class has. For instance:
classdef book
properties
title
end
methods
function obj = book(title)
obj.title = title;
end
end
end
Objects of class book have a title property.
whale = book('Moby Dick');
The whale object is a book and this specific book object has the title 'Moby Dick: the whale object's title property has value 'Moby Dick'.
mist = book('Mistborn');
The mist object is a book and this specific book object has the title 'Mistborn': the mist object's title property has value 'Mistborn'.
If you were to ask "What's the title of a book?" that's not really answerable. If you were to ask "What's the title of the book object stored as the variable whale?" that's easily answerable: 'Moby Dick'.
Stijn Haenen
am 30 Jul. 2020
If you have these two classes:
classdef clwinding
properties
d
w
end
methods
function objw = clwinding (d, w)
objw.d = d;
objw.w = w;
end
end
end
classdef clloss
properties
d
w
s
end
methods
function r = get_s(~,obj)
r = obj.w+obj.d;
end
end
end
And you run this script, it should work I think:
a=clwinding(1,2);
b=clloss;
get_s(b,a);
2 Kommentare
Stijn Haenen
am 6 Aug. 2020
Have you saved the two classes in two different files?
I have no other idea what might cause this error
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!