geting started with oop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
per isakson
el 9 de Mzo. de 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 comentarios
per isakson
el 9 de Mzo. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Array Geometries and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!