dart object.class code example

Example 1: object in dart

// Class declaration
class Car {
  
  String model;
  int year;
  var serialNumber;
  
  // Constructor
  Car(this.model, this.year, this.serialNumber);
  
}

// Follow me on Grepper for any future Dart Questions! Hope this Helps...
void main() {
  
  // Creating Object
  Car Honda = new Car('Civic', 2021, 'HZ326Y');
  
  print(Honda.year);
}

/* Syntax
class class_name {  
   <fields> 
   <getters/setters> 
   <constructors> 
   <functions> 
} */

Example 2: dart class

// entry point
void main(){

  // using the Dog class - creating an instance of this class
  var myDog = Dog();
  
  // assigning values to the newly clreated instance
  myDog.breed = 'Poodle';
  myDog.name = 'Jack';
  myDog.color = 'Brown';
 
  // displaying the values on the console 
  print(myDog.breed);
  print(myDog.name);
  print(myDog.color);
}

// Dog class 
class Dog{

  // class properties
  String breed;
  String name;
  String color;
}

Tags:

Dart Example