Classes

Jun's Coding Journey·2023년 8월 10일
0

[Learn] Dart

목록 보기
6/6

Dart Class


In Dart, a class is a blueprint for creating objects. It defines the structure and behavior of objects that belong to that class.

We explicitly declare the types when defining properties in classes.

Using instances will create an object from the classes.

For Dart classes, the this keyword is not required.

class Player {
  String name = 'John';
  int xp = 1500;
  
  void sayHello() {
  	print('Hi my name is $name');
  }
}

void main() {
  var player = Player();
  print(player.name); // John
  player.name = 'Dave';
  print(player.name); // Dave
  player.sayHello();
}

 

Constructors


In Dart, a constructor is a special type of method within a class that is responsible for creating and initializing instances (objects) of that class. Constructors define how object properties are set when an instance is created.

When using constructors, add the late keyword to specify that the values will be received on the constructor.

class Player {
  late String name;
  late int xp;
  
  Player(String name, int, xp) {
  	this.name = name;
    this.xp = xp;
  }
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

void main() {
  var player = Player("John", 1500);
  player.sayHello();
  var player2 = Player("Dave", 2500);
  player2.sayHello();
}

 
Less-repetitive way of creating constructors(recommended):

class Player {
  String name;
  int xp;
  
  Player(this.name, this.xp);
}

 

Named Constructor Parameters


Use named arguments to organize potentially large classes and implement better readability.

Use required keyword to avoid null errors.

class Player {
  String name;
  int xp;
  String team;
  int age;
  
  Player({
  	required this.name,
    required this.xp,
    required this.team,
    required this.age,
  });
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

void main() {
  var player = Player(
  	name: "John",
    xp: 1200,
    team: "blue",
    age: 21,
  );
  var player2 = Player(
  	name: "Dave",
    xp: 2500,
    team: "red",
    age: 12,
  );
}

 

Named Constructors


Using named constructors, we can customize and create a variety constructors that can function for different purposes.

Able to set user and default values separately.

class Player {
  final String name;
  int xp, age;
  String team;
  
  Player({
  	required this.name,
    required this.xp,
    required this.team,
    required this.age,
  });
  
  Player.createBluePlayer({
  	required String name,
    required int age,
  }) : this.age = age,
  	   this.name = name,
       this.team = "blue",
       this.xp = 0;
  
  Player.createRedPlayer({
  	required String name,
    required int age,
  }) : this.age = age,
  	   this.name = name,
       this.team = "red",
       this.xp = 0;
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

void main() {
  var bluePlayer = Player.createBluePlayer(
  	name: "John",
    age: 21,
  );
  var redPlayer = Player.createRedPlayer(
  	name: "John",
    age: 21,
  );
}

 
Example: "Converting a list fetched API data into classes"

class Player {
  final String name;
  int xp, age;
  String team;
  
  Player.fromJson(Map<String, dynamic> playerJson) 
  : name = playerJson['name'],
    xp = playerJson['xp'],
    team = playerJson['team'],
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

void main() {
  var apiData = [
  	{
      "name": "John",
      "team": "red",
      "xp": 0,
    }
    {
      "name": "Dave",
      "team": "red",
      "xp": 0,
    }
    {
      "name": "Matt",
      "team": "red",
      "xp": 0,
    }
  ];
  
  apiData.forEach((playerJson) {
  	Player.fromJson(playerJson);
    player.sayHello();
  });
}

 

Cascade Notation


A shorter way to modify variables without being too repetitive.

Use .. to link to the target variable and use semi-colon on the very last modifier.

class Player {
  String name;
  int xp;
  String team;
  
  Player({
  	required this.name,
    required this.xp,
    required this.team,
  });
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

void main() {
  var john = Player(
    name: "John",
    xp: 1200,
    team: "red",
  )
  ..name = 'las'
  ..xp = 1200000
  ..team = 'blue'
  ..sayHello();
}

 

Enums


Enums help developers avoid mistakes of misspelling variables or values.

Dart and Flutter have their own enums for us to use (very easy to use).

enum Team { red, blue }

class Player {
  String name;
  int xp;
  Team team;
  
  Player({
  	required this.name,
    required this.xp,
    required this.team,
  });
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

void main() {
  var john = Player(
    name: "John",
    xp: 1200,
    team: Team.red,
  )
  ..name = 'las'
  ..xp = 1200000
  ..team = Team.blue
  ..sayHello();
}

 

Abstract Classes


In Dart, an abstract class is a class that cannot be directly instantiated but serves as a blueprint for other classes.

By extending an abstract class from another class, we gain access to all its methods.

abstract class Human {
  void walk();
}

enum Team { red, blue }

class Player extends Human {
  String name;
  int xp;
  Team team;
  
  Player({
  	required this.name,
    required this.xp,
    required this.team,
  });
  
  void walk() {
  	print("I'm walking");
  }
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

class Coach extends Human {
  void walk() {
  	print("The coach is walking");
  }
}

void main() {
  var john = Player(
    name: "John",
    xp: 1200,
    team: Team.red,
  )
  ..name = 'las'
  ..xp = 1200000
  ..team = Team.blue
  ..sayHello();
}

 

Inheritance


Inheritance is a fundamental concept in object-oriented programming (OOP), including Dart. It allows you to create a new class based on an existing class, inheriting its properties and behaviors.

Use the super keyword to talk to the extended class.

With the @override keyword, we can modify properties or methods from the extended classes.

class Human {
  final String name;
  Humnan({required this.name});
  
  void sayHello() {
  	print("Hi my name is $name");
  }
}

enum Team { blue, red }

class Player extends Human {
  final Team team;
  
  Player({
  	required this.team,
    required String name,
  }) : super(name: name);
  
   // modifying the original methods from classes
  void sayHello() {
  	super.sayHello();
    print("and I play for ${team}");
  }
}

void main() {
  var player = Player(
  	team: Team.red,
    name: "John",
  );
}

 

Mixins


Mixins are classes that have no constructors and they are used add properties to classes.

Unlike extends, where class becomes a child of the extended class, mixins allow us to directly get methods without creating a constructor.

class Strong {
  final double strengthLevel = 1500.99;
}

class QuickRunner {
  void runQuick() {
  	print("ruuuuuuun!");
  }
}

class Tall {
  final double height = 1.99;
}

enum Team { blue, red }

class Player with Strong, QuickRunner, Tall {
  final Team team;
  
  Player({
  	required this.team,
  });
}

class Horse with Strong, QuickRunner {}

class Kid with QuickRunner {}

void main() {
  var player = Player(
  	team: Team.red,
    name: "John",
  );
  player.runQuick();
}

profile
Greatness From Small Beginnings

0개의 댓글