• Classes

    A class in programming is like a group of people. For example, if you have a group of friends, each person in the group is called a "friend". In programming, we call each person in the group a "class". Just like your friends might all have different names and personalities, classes can also have different names and qualities. So when you write code, you can create different classes with different names and abilities to do things.

      • Object Classes

      • class Person {
          String name;
          int age;
        
          Person(this.name, this.age);
        
          void greet() {
        	print('Hello my name is ' + this.name);
          }
        }
        
        void main() {
          var human = new Person('John', 36);
        
          print(human.name); // John
          print(human.age); // 36
          human.greet(); // Hello my name is John
        }
        
      • Trait Classes

      • mixin HasFlippers {
          String swim() {
        	return 'can swim';
          }
        }
        
        mixin HasFeathers {
          String fly() {
        	return 'can fly';
          }
        }
        
        class Duck with HasFlippers, HasFeathers {}
        
        class Seal with HasFlippers {}
        
        void main() {
          var a = new Duck();
          print(a.swim()); // can swim
          print(a.fly()); // can fly
        
          var b = new Seal();
          print(b.swim()); // can swim
        }
        
      • Interface Classes

      • import "dart:math" show pi;
        
        abstract class Shape {
          double area();
        }
        
        class Rectangle implements Shape {
          double width;
          double height;
        
          Rectangle(this.width, this.height);
        
          double area(){
        	return this.width * this.height;
          }
        }
        
        class Circle implements Shape {
          double radius;
        
          Circle(this.radius);
        
          double area(){
        	return pi * this.radius * this.radius;
          }
        }
        
        void main() {
          Rectangle a = new Rectangle(3, 5);
          Circle b = new Circle(4);
        
          print("area of a is ${a.area().toStringAsPrecision(8)}");  // area of a is 15.000000
          print("area of b is ${b.area().toStringAsPrecision(8)}");  // area of b is 50.265482
        }
      • Struct Classes

      • class Person {
          String name;
          int age;
        
          Person(this.name, this.age);
        }
        
        void main() {
          var human = new Person('John', 36);
        
          print(human.name); // John
          print(human.age); // 36
        }
      • Enum Classes

      • enum Days {
        	sunday,
        	monday,
        	tuesday,
        	wednesday,
        	thursday,
        	friday,
        	saturday
        }
        
        void main() {
        	var day = 4;
        
        	if (day == Days.saturday.index || day == Days.sunday.index) {
        	print('a weekend');
        	} else {
        	print('a weekday'); // a weekday
        	}
        }