geting started with oop

3 views (last 30 days)
jeff wu
jeff wu on 9 Mar 2012
hi everybody, i got a probally simple question but would be gratefull if anyone could help
i got the following code (that does not work)
classdef Knot
properties
x
y
end % properties
methods
function K = Knot(x,y)
K.x=x;
K.y=y;
end%functions
end%methods
end %class
classdef Slab<Knot
properties
Material
StartKnot
EndKnoten
end%properties
methods
function S = Slab(StartKnot,EndKnot)
S.StartKnot = StartKnot;
S.EndKnot = EndKnot;
end%function
end%methods
end%class
in my mind the subclass Slab should get the properties x and y.
trying to run it gives me this:
>> k1 = Knoten(1,1);
>> k2 = Knoten(2,2);
>> s= Stab(k1)
??? Input argument "x" is undefined.
Error in ==> Knoten>Knoten.Knoten at 8
K.x=x;
Error in ==> Stab>Stab.Stab at 8
function S = Stab(StartKnoten)
acctually i wanted to get a Slab class that has the properties of Knot and could be acceses like this:
k=Knot(1,1);kk = Knot(1,2);S = Slab(k1,k2);
Slab.StartKnot.x should deliver the x propertie of Knot
obviously i got something wrong
thankfull for help

Accepted Answer

per isakson
per isakson on 9 Mar 2012
Firstly, you need to be more careful with the names. Should it be Knot or Knoten and Slab or Stab.
Secondly, the constructor of Knot requires values of the two inputs. Add the line:
if nargin == 0, return, end
to Knot
>> clear all, clear classes
>> k1 = Knot(1,1);
>> k2 = Knot(2,2);
>> s = Slab(k1,k2)
s =
Slab
Properties:
Material: []
StartKnot: [1x1 Knot]
EndKnot: [1x1 Knot]
x: []
y: []
Methods, Superclasses
>> s.StartKnot.x
ans =
1
Thirdly, do you really want Slab both to inherit Knot and have Knot objects as values of properties?
  2 Comments
jeff wu
jeff wu on 9 Mar 2012
thanks alot for your answer, it works now but i dont get why.when i create a StabObject i dont call the knot function? or is this because stab inherist all of the knot?
i want them to be inherited both because x and y could change of an knoten element without the stab knowing about it and otherwise. and i want to be able to call the knotcoordinates within stab.
thanx again
per isakson
per isakson on 9 Mar 2012
When you create a Slab object the Knot constructor is called automatically because Slab inherits Knot. Matlab works that way.
I guess you need to read carefully about the difference between "handle" and "value" classes/objects.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!