I apologize if my title is not clear. I mean if I have a class that does something and returns a string, could I do something like this?
example:String = new MyClassThatReturnsStrings(1234);
If I cannot, how do I handle returns for a class? Thanks
A constructor can't return a value. You need to move the calculation that return something in another method other than constructor and after instantiating the class you need to call that method. Like this:
var myClass:MyClass = new MyClass();
var example:String = myClass.methodThatCalculateAndReturn(1234);
You can do this in one line if you don't need the object in anywhere else. Like this:
var example:String = (new MyClass()).methodThatCalculateAndReturn(1234);
As an alternative to taskinoor's answer , you could define the method as static
:
public class MyClass
{
public function formatNumber(number : Number) : String
{
return number.toString();
}
}
var value : String = MyClass.formatNumber(1234);
Or you can skip the class entirely and declare the function globally:
package com.package.name
{
public function formatNumber(number : Number) : String
{
return number.toString();
}
}
var value : String = formatNumber(1234);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.