Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. class Animal{
  2. // 1. initialize at the declaration
  3. num age = 1;
  4. // 2. assign variable in the constructor
  5. num leg;
  6. Animal(this.leg);
  7. }
  8.  
  9. class Plant{
  10. // 3. use the initialozer list
  11. num weight;
  12. num height;
  13. Plant(weight,height):this.weight = weight,this.height = height;
  14. }
  15.  
  16. class Human{
  17. // 4. no-final variable can be initialize in the constructor.
  18. final String name;
  19. num age;
  20. Human(name,age):this.name = name{
  21. this.age = age;
  22. }
  23.  
  24. // The constructor below will casue a compile error because a final variable do not have a setter.
  25. // this.name = name;
  26. // Human(name,age){
  27. // this.age = age;
  28. // this.name = name;
  29. // }
  30.  
  31. }
  32.  
  33. void main (){
  34. Animal animal = new Animal(4);
  35. Plant plant = new Plant(1,2);
  36. Human human = new Human("Tom", 14);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement