Talk:Object Oriented Programming with Nasal
Jump to navigation
Jump to search
Creating an empty class template
var myClass = { # body of your hash };
Adding a constructor
This is added to the body of your hash:
# constructor, for making new objects new: func( arg... ) { # creates a temporary object that inherits from myClass # what is obj here, will later on be 'me' in the actual object var obj = { parents:[ myClass ] }; # do any other object setup here, such as for example obj.message = "Hello World"; print("Creating new object/instance of type/template:myClass"); # return the object to the caller return obj; },
Adding a destructor
This is added to the body of your hash:
# destructor, for deleting objects del: func( arg... ) { # add your cleanup code here print("Deleting object of type/template: myClass"); return nil; },
Adding a method
This is added to the body of your hash:
# declare a method named myMethod myMethod: func(arg...) { # do your processing here return nil; },
Using inheritance
This is added to the body of your hash:
parents:[ myClass ]
Creating a Radar class
# declare a class named RadarTarget
var RadarTarget = {
# constructor, for making new objects
new: func( arg... ) {
# creates a temporary object that inherits from RadarTarget
# what is obj here, will later on be 'me' in the actual object
var obj = { parents:[ RadarTarget ] };
# do any other object setup here, such as for example
obj.message = "Hello World";
print("Creating new object/instance of type/template:RadarTarget");
# return the object to the caller
return obj;
},
# destructor, for deleting objects
del: func( arg... ) {
# add your cleanup code here
print("Deleting object of type/template: RadarTarget");
return nil;
},
# declare a method named sayHello
sayHello: func(name) {
# do your processing here
print("Hello ",name);
},
# declare a method named setPos
setPos: func(lat,lon) {
# do your processing here
me.lat=lat; me.lon=lon;
},
# declare a method named getPos
getPos: func() {
# do your processing here
return [me.lat,me.lon];
},
};
# now, let's try to create a new object
var myObject=RadarTarget.new( nil );
# call the object's setter method:
myObject.setPos( lat: 22.00, lon: 66.00 );
# call the object's getter method to extract the values
var ( lat,lon ) = myObject.getPos();
# you can access all members/fields of the class by using the '''me.''' prefix now
# add generic example here
# to delete an object, just call the '''.del()''' method:
# we will be doing this using a timer, 5 seconds later:
settimer( func
myObject.del( nil )
,5.00);
Creating a Point2D class
# declare a class named Point2D var Point2D = { # constructor, for making new objects new: func( arg... ) { # creates a temporary object that inherits from Point2D # what is obj here, will later on be 'me' in the actual object var obj = { parents:[ Point2D ] }; # do any other object setup here, such as for example obj.message = "Hello World"; print("Creating new object/instance of type/template:Point2D"); # return the object to the caller return obj; }, # destructor, for deleting objects del: func( arg... ) { # add your cleanup code here print("Deleting object of type/template: Point2D"); return nil; }, # declare a method named setValue setValue: func(x,y) { # do your processing here me.x=x;me.y=y; }, # declare a method named getValue getValue: func() { # do your processing here return [me.x,me.y]; }, }; # now, let's try to create a new object var somePoint=Point2D.new( nil ); # call the object's setter method: somePoint.setValue( x: -124.40, y: 240.34 ); # call the object's getter method to extract the values var ( x,y ) = somePoint.getValue(); # you can access all members/fields of the class by using the me. prefix now # add generic example here # to delete an object, just call the .del() method: # we will be doing this using a timer, 5 seconds later: settimer( func somePoint.del( nil ) ,5.00);
Creating a Graph2DAxis class
# declare a class named Graph2DAxis var Graph2DAxis = { # constructor, for making new objects new: func( arg... ) { # creates a temporary object that inherits from Graph2DAxis # what is obj here, will later on be 'me' in the actual object var obj = { parents:[ Graph2DAxis ] }; # do any other object setup here, such as for example obj.message = "Hello World"; print("Creating new object/instance of type/template:Graph2DAxis"); # return the object to the caller return obj; }, # destructor, for deleting objects del: func( arg... ) { # add your cleanup code here print("Deleting object of type/template: Graph2DAxis"); return nil; }, # declare a method named setMinMax setMinMax: func(min,max) { # do your processing here me.min=min;me.max=max; }, # declare a method named getMinMax getMinMax: func() { # do your processing here return [me.min,me.max]; }, # declare a method named setColor setColor: func(color) { # do your processing here me.color=color; }, # declare a method named getColor getColor: func() { # do your processing here return me.color; }, }; # now, let's try to create a new object var myObject=Graph2DAxis.new( nil ); # call the object's setter method: myObject.setMinMax( min: -100.00, max: 200.00 ); # call the object's getter method to extract the values var ( min,max ) = myObject.getMinMax(); # you can access all members/fields of the class by using the me. prefix now # add generic example here # to delete an object, just call the .del() method: # we will be doing this using a timer, 5 seconds later: settimer( func myObject.del( nil ) ,5.00);
Creating a PropertyPlotter class
# declare a class named PropertyPlotter var PropertyPlotter = { # constructor, for making new objects new: func( arg... ) { # creates a temporary object that inherits from PropertyPlotter # what is obj here, will later on be 'me' in the actual object var obj = { parents:[ PropertyPlotter ] }; # do any other object setup here, such as for example obj.message = "Hello World"; print("Creating new object/instance of type/template:PropertyPlotter"); # return the object to the caller return obj; }, # destructor, for deleting objects del: func( arg... ) { # add your cleanup code here print("Deleting object of type/template: PropertyPlotter"); return nil; }, # declare a method named setConfig setConfig: func(name, property, factor, top, line, color) { # do your processing here me.name=name; me.property=property; me.factor=factor; me.top=top; me.line=line; me.color=color; }, # declare a method named getConfig getConfig: func() { # do your processing here return [me.name,me.property,me.factor,me.top,me.line,me.color]; }, # declare a method named update update: func() { # do your processing here setprop(me.property, "name", me.name); setprop(me.property, "factor", me.factor); setprop(me.property, "top", me.top); setprop(me.property, "line", me.line); setprop(me.property, "color", me.color); }, }; # now, let's try to create a new object var myPlot=PropertyPlotter.new( nil ); # call the object's setter method: myPlot.setConfig( name:"frame-spacing", property:"/sim/frame-latency-max-ms",factor:-1000,top:180,line:1,color:"#ff0000" ); # call the object's getter method to extract the values var ( name,property,factor,top,line,color ) = myPlot.getConfig(); # you can access all members/fields of the class by using the me. prefix now # add generic example here # to delete an object, just call the .del() method: # we will be doing this using a timer, 5 seconds later: settimer( func myPlot.del( nil ) ,5.00);