Search code examples
apache-flex

adobe flex create child object given a parent object


i have the following 2 classes

class A {
    var one:int;
}
class B extends A {
    var two:int;
}

I now have an object of the class A but need to create and object of class B and set the additional parameters.

Does Flex allow child object creation given a parent object ?

note: there are lot of parameters in A so i don't want to copy individually each one.


Solution

  • Flex does allow multiple constructors, but it would take the form of copying each parameter in the constructor.

    public class B extends A {
        var two:int;
    
        public function B(instanceA:A) {
            this.one = instanceA.one;
            //etc
        }
    }
    

    Another approach could be to use ObjectUtil:

    public class B extends A {
        var two:int;
    
        public function B(instanceA:A) {
            var data = ObjectUtil.getClassInfo(instanceA);
    
            for each (var prop in data.properties) {
                this[prop] = instanceA[prop];
            }
        }
    }
    

    n.b. Check the excludes and options arguments of getClassInfo -- if there are properties that would cause problems to be processed in this way, you'll want to make sure to exclude them.