maxkleiner / maXbox4

code compiler script studio
GNU General Public License v3.0
23 stars 10 forks source link

Static Methods #19

Closed maxkleiner closed 1 year ago

maxkleiner commented 4 years ago

One rule-of-thumb: ask yourself "Does it make sense to call this method, even if no object has been constructed yet?" If so, it should definitely be static.

So in a class Car you might have a method:

double convertMpgToKpl(double mpg)

...which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):

void setMileage(double mpg)

...can't be static since it's inconceivable to call the method before any Car has been constructed.

Or a bitmap instead of a car with a utility function:

FPicRed:= TBitMap.Create; FPicled.LoadFromResourcename(Hinstance, 'yellow1'); FPicRed.LoadFromFile(Exepath+'\examples\images\red1.bmp');

Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions.

maxkleiner commented 2 years ago

Or you put just methods in a class without specific constructor: procedure SIRegister_TAzuliaStrings(CL: TPSPascalCompiler); begin //with RegClassS(CL,'TOBJECT', 'TAzuliaStrings') do with CL.AddClassN(CL.FindClass('TOBJECT'),'TAzuliaStrings') do begin Function NPos( C : Char; S : string; N : Byte) : Byte'); Function IntToRoman( Value : Longint) : string'); Function RomanToInt( const S : string) : Longint'); Function GetDirFromStr( DirectoryString : string; IndexFromRight : integer) : string'); Function RemoveChars( InputString : string; CharToRemove : Char) : string'); Function GetStringFromGarbage( InputString : string; Index : integer) : string'); Function SplitupWithGarbage( InputString : string; CommonSplitter : char) : string'); Function GetCount( Character : char; InputString : string) : integer'); Function RemoveTrailingChars( StringToRemoveFrom : string; Trailer : char) : string'); end; end;

maxkleiner commented 1 year ago

Answer9:

No, maXbox4 does not have static methods.