I have browsed the forum but could not find a solution for the below (i.e a class with overloaded constructors). a B4J class does require the default Initialize method (can modify to pass parameters) but how would one go about to allow for all 3 the constructors in a B4J class as per java code below?
Sub Class_Globals
Private x As Double
Private y As Double
Private norm As Double
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (obj() As Object)
If obj = Null Then
x = 0
y = 0
norm = 0
else if obj.Length = 1 Then
x = obj(0).As(MyClass).x
y = obj(0).As(MyClass).y
else if obj.Length = 2 Then
x = obj(0).As(Double)
y = obj(1).As(Double)
End If
End Sub
Sub setX(xv As Double)
x = xv
End Sub
Sub setY(yv As Double)
y = yv
End Sub
Sub getX As Double
Return x
End Sub
Sub getY As Double
Return y
End Sub
The caller
B4X:
Sub Process_Globals
Dim mm, mm1, mm2 As MyClass
End Sub
Sub AppStart (Args() As String)
mm.Initialize(Null)
Log("mm = " & mm)
mm1.Initialize(Array(5.0, 10.0))
Log("mm1 = " & mm1)
mm2.Initialize(Array(mm1))
Log("mm2 = " & mm2)
End Sub