Page 15 - Dart Language
P. 15

Chapter 3: Classes




        Examples



        Creating a class


        Classes can be created as follow:


         class InputField {
           int maxLength;
           String name;
         }


        The class can be instantiated using the new keyword after which the field values will be null by
        default.


         var field = new InputField();


        Field values can then be accessed:


         // this will trigger the setter
         field.name = "fieldname";

         // this will trigger the getter
         print(field.name);



        Members


        A class can have members.

        Instance variables can be declared with/without type annotations, and optionally initialized.
        Uninitialised members have the value of null, unless set to another value by the constructor.


         class Foo {
           var member1;
           int member2;
           String member3 = "Hello world!";
         }


        Class variables are declared using the static keyword.


         class Bar {
           static var member4;
           static String member5;
           static int member6 = 42;
         }


        If a method takes no arguments, is fast, returns a value, and doesn't have visible side-effects, then




        https://riptutorial.com/                                                                               10
   10   11   12   13   14   15   16   17   18   19   20