metadeck
10/5/2018 - 10:36 AM

Beginners Dart - Scope

Beginners Dart - Scope

class Animal {

  String name = "Unknown";

  Animal(String name) {
    //Here we have defined a parameter that the constructor will accept, yet we have defined that name twice more within the constructor meaning that this can get confusing quite fast. At this point this code actually assigns name to the class variable's definition of name using this.name, as it was last on the list. ("UNKNOWN")

    //String name = "Hello";

    this.name = name;
    //Here we see that the printed name is unknown because of the keyword this and that our cat variable in main is unused.
    print(this.name); 

  }

}

void main() {
  
   Animal cat = new Animal("Fluffy");

    for(int i = 0; i < 10; i++) {

      print(i);

      if(i < 8) {

        // int i = 5; Here we see se that with this line added to the code that it effectively will set i to 5 while i < 8, breaking the technical functionality of the program, causing it to print after every number.

        if(i > 4) {

          print("i is <8 and > 4");

        }
      }
    }

}