4 Replies Latest reply on Dec 21, 2010 12:24 PM by GordonSmith

    Functions / Classes, when to use them...

    SpaghettiCoder Level 3

      Are classes simply functions nicely packaged?

       

      When would you want to use a class rather than a function...

       

      If your project isn't overly complex should you bother with creating classes?

        • 1. Re: Functions / Classes, when to use them...
          SpaghettiCoder Level 3

          Let me get this straight, Constructors are executed everytime a class is created?

           

          1. you can execute a block of code automatically etc...

          2. Constructors don't return data

           

          If constructors can't return data, are most constructors just left blank???

          Is the real power in the methods that can return data???

          • 2. Re: Functions / Classes, when to use them...
            saisri2k2 Level 4

            SpaghettiCoder wrote:

             

            Let me get this straight, Constructors are executed everytime a class is created?

             

            1. you can execute a block of code automatically etc...

            2. Constructors don't return data

             

            If constructors can't return data, are most constructors just left blank???

            Is the real power in the methods that can return data???

            yes, constructtors are called everytime you try to create object out of a class. yes, most of the constructors are left blank, unless you want to set some properties by the time the display object is created.

             

             

            eg: If I want the UI to get data for user 1 in Registration screen, so, I set the User id using constructor and prepare the HttpService in the constructor like the following

             

             

            var userRegistration:Registration = new Registration(1,'http://someurl:8080/path');

             

            ---------

             

            in Registration.mxml/.as code

             

            Class Registration{
            
                 var _http:HttpService = null;
                 public Registration(uid:int,path:String){
                      _http = //inithttp
                      _http.id = uid;
                      //see, here you are not calling the server, only initializing a few things of the class
                      // this constructor does not need to return anything(not allowed usually)
                 }
            
            }
            
            1 person found this helpful
            • 3. Re: Functions / Classes, when to use them...
              saisri2k2 Level 4

              SpaghettiCoder wrote:

               

              Are classes simply functions nicely packaged?

               

              When would you want to use a class rather than a function...

               

              If your project isn't overly complex should you bother with creating classes?

              No to the first one question. Functions are parts of the classes, so, second question is invalid(i dont know how the functions/methods can sustain on their own)? Even if your project is small, please try to create classes, Even if you are creating a custom component, you are actually creating a class. By using classes, you are writing most reusable code = low on maintanace.

              • 4. Re: Functions / Classes, when to use them...
                GordonSmith Level 4

                In Object-Oriented Programming ("OOP"), a class represents a category of objects. Subclasses represented more-specific categories. For example, you might have a Pet class with subclasses Cat, Dog, and Ferret.

                 

                The properties of the class (implemented as variables or as getter/setters) store the state of the object, such as 'age', 'weight', and 'color'. Different "instances" of the class (created with the 'new' operator) will generally have different values for their properties, because in the real world cats have different ages, weights, and colors.

                 

                The functions in the class (also called its methods) implement the actions that the object can perform, such as eat() and sleep(). How these actions are implemented typically depend on the properties. For example, a pet that weights more probably eats more.

                 

                So a class is not just a set of "nicely packaged" functions. Classes are how you represent hierarchical categories of things that store state and perform actions.

                 

                Gordon Smith

                Adobe Flex SDK Team

                1 person found this helpful