Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Animal{
- // 1. initialize at the declaration
- num age = 1;
- // 2. assign variable in the constructor
- num leg;
- Animal(this.leg);
- }
- class Plant{
- // 3. use the initialozer list
- num weight;
- num height;
- Plant(weight,height):this.weight = weight,this.height = height;
- }
- class Human{
- // 4. no-final variable can be initialize in the constructor.
- final String name;
- num age;
- Human(name,age):this.name = name{
- this.age = age;
- }
- // The constructor below will casue a compile error because a final variable do not have a setter.
- // this.name = name;
- // Human(name,age){
- // this.age = age;
- // this.name = name;
- // }
- }
- void main (){
- Animal animal = new Animal(4);
- Plant plant = new Plant(1,2);
- Human human = new Human("Tom", 14);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement