Oxygen Basic
Programming => Problems & Solutions => Topic started by: pber on June 30, 2014, 03:31:09 AM
-
I know I'll be happy if someone will enlight me
about two semanthics around classes.
method add(sys n,= double x, y)
and
/\
...for the latter I'm not too optimistic.
Thanks
-
Hello Frank,
class cAstroUnit
double a
double b
METHOD constructor(a1 AS DOUBLE)
a = a1
b = a1*149597870691/1000
END METHOD
METHOD ToString() AS STRING
return a ", " b " meters = " b " kilometers"
END METHOD
END CLASS
cAstroUnit cA
cA.constructor = 123
print "result: " cA.ToString()
-
Hello Peter ::)
-
Yes, that illustrates the simple use of a class.
/\ is low-level semantics, mostly for internal use. It separates the static components from the dynamic components in the Class type descriptor. For example:
class foo
static double sd1,sd2,sd3
double d1,d2,d3
method m1(double m) as double
return (sd1+sd2+sd3)*m
end method
method m2(double m) as double
return (d1+d2+d3)*m
end method
end class
foo fooObj
print FooObj.m1(10)
The compiler will expand the above class definition to a class type definition and a group of procedures:
class foo
static double sd1,sd2,sd3
method m1(double m) as double
method m2(double m) as double
/\
double d1,d2,d3
end class
methods of foo
method m1(double m) as double
return (sd1+sd2+sd3)*m
end method
'
method m2(double m) as double
return (d1+d2+d3)*m
end method
end methods
This syntax is available to the programmer but seldom necessary :)
-
method add(sys n,= double x, y)
This syntax is available for invoking pseudo-variables. It is not essential but requires the programmer to call the method with pseudo-variable syntax.
class foo
double v[100]
method sum(int n,= double a,b,c)
v[n]=a+b+c
end method
method sum(int n) as double
return v[n]
end method
end class
foo j
j.sum(4)=1.5, 2.5, 3.5
print j.sum(4) '7.5
Without enforced pseudo-variable syntax:
class foo
double v[100]
method sum(int n, double a,b,c)
v[n]=a+b+c
end method
method sum(int n) as double
return v[n]
end method
end class
foo j
j.sum(4)=1.5, 2.5, 3.5 'accepted
j.sum(4,1.5, 2.5, 3.5) 'also accepted
print j.sum(4) '7.5
-
thanks peter for your help :) I am not very fit with class constructions so I am learning yet
here's my final solution for my task...
' correct :) --------------------------------- //
class cAstroUnit2
double a
double b
double c
METHOD constructor(a1 AS DOUBLE)
a = a1
'b = a1*149597870691/1000
b = a1*149597870691
c = b/1000
END METHOD
METHOD ToString() AS STRING
'return a ", " b " meters = " b " kilometers"
return str(a)+", "+str(b) +" meters = " +str(c) +" kilometers"
END METHOD
END CLASS
cAstroUnit2 cA
cA.constructor = 1 '2
print "result: " cA.ToString()
thanks charles too for further explanations.
thanks, frank
-
Thanks Charles,
at the beginning I misunderstood how methods get defined,
it seemed to me you can re-define a single method of an existing class.
No i realize you could redefine all methods as a whole
(if /\
+ methods of
are used).
But I guess this is not the way you thought class methods.