Advertisement
khoi01

1.Intro to Classes (Dart)

Jan 25th, 2020
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.60 KB | None | 0 0
  1.  
  2.  
  3. class Microphone{
  4.  
  5.   //Instance variables, member variables
  6.   //this = this object/class
  7.    String name;
  8.    String color;
  9.    int model = 4536;
  10.  
  11.  
  12.    //Syntactic sugar constructor
  13.    Microphone(this.name, this.color, this.model);
  14.  
  15. // Named Constructor
  16.    Microphone.initialize() {
  17.       name = "Blue Yeti 2nd Edition";
  18.       model = 67;
  19.  
  20.  
  21.  
  22.    }
  23.  
  24.  
  25.    String get getName => name; // getter
  26.    set setName(String value) => name = value; // setter
  27.  
  28.  
  29.  
  30.  
  31.  
  32.    //constructor
  33. //   Microphone(String name, String color, int model) {
  34. //
  35. //       this.name = name;
  36. //       this.color = color;
  37. //       this.model = model;
  38. //   }
  39.  
  40.    //Microphone(this.name, this.color, this.model);
  41.  
  42.    void turnOn() {
  43.      print("$name is on!");
  44.  
  45.    }
  46.  
  47.    bool isOn() => true;
  48.  
  49.    int modelNumber() => model;
  50.  
  51.  
  52.  
  53.    void turnOff() {
  54.      print("$name is turned off!");
  55.    }
  56.  
  57.    void setVolume() {
  58.      print("$name with color: $color volume is up!");
  59.    }
  60.  
  61.  
  62.  
  63.  
  64. }
  65.  
  66.  
  67.  
  68. main(List<String> arguments) {
  69.  
  70.   var mic = new Microphone("Blue Yeti", "Silver gray", 1345); // we are crating the actual object of type mic
  71.  
  72.   mic.setName = "NewName";
  73.  
  74.   String name = "Paulo";
  75.  
  76.  
  77.   print(mic.getName);
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. //  var micSecond = new Microphone.initialize(); // using the initialize named constructor
  97. //
  98. //  print(micSecond.model);
  99. //  print(micSecond.name);
  100. //
  101. // // print(mic.model); //. (dot) access operator
  102. //  mic.model = 8837647;
  103. //
  104. //  mic.turnOn();
  105. //  mic.setVolume();
  106. //  mic.turnOff();
  107. //
  108. //  print(mic.isOn());
  109. //  print(mic.modelNumber());
  110.  
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement