Classes have lifecycles
class Animal { String _name =' ';
void sayHello() { if (_name.isEmpty) { print('Hello'); } else { print('Hello ${_name} nice to meet you'}; } } }
A Constructor is the funciton called when a class is created sayHello() <== the parentheses are the constructor, empty is a default constructor Animal (String name) <== custom constructor with one requirement
this keyword => a pointer to the existing object, not a new object
Lexical Scope: Variables are available top down If you define a variable inside of a function, then it overrides the this. reference The variable in the constructor is overridden and becomes a parameter becuase the variable is in the function Do not reuse variable names or you will have problems
Getters get information Setters set information
class Animal {
String _name; int _age = 0;
Animal(String name, int age) { _name = name; _age = age * 7; }
String get name => _name; //Getter - gets private variable from inside of a class void set name(String value) => _name = value; //Setter
int get age => _age; //Getter void set age(int value) => _age = value * 7; //Setter } Getters and Setters allow you to do some manipulations
Static is shared across all instances of a class. The variable will be the same no matter how many instances of the class you have (without static, each class would have their own version of the variable). This is also true of functions. Static does not have a .this If you want something available at the class level, introduce as a static variable/function
class YYY extends XXX (Class YYY adds properties to the properties already in class XXX) Class YYY inherits the properties of class XXX
super is the class we are inheriting from
Cannot extend from multiple classes (some languages allow this) Instead, use the with key word class Dragon extends Mammal with Feline, Dog { } Have to be careful with functions used in Mixins because they don't play nicely together. Can be confusing
A contract between two classes Inherit takes all of the properties of the super class When you implement an interface, you have to build everything yourself from the ground up
Abstract is more of a concept than a class. Functions inside do not need a function body When extending an abstract class, you must then provide the function body for any funcitons in the abstract class
List numbers = new List(); numbers.addAll([1,2,3,4]); print(numbers);
List strings = new List(); strings.addAll(['a', 'b', 'c']); print(strings);
This List code can handle different types depending on what you want it to do
Synchronous - things happen one at a time - standing in line at the bank Asynchronous - thing shappen all at once - like opening the gates at a fair