Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Last active November 21, 2018 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicbeacon/7921da9f0c99d15a19a6cf0a6e329f32 to your computer and use it in GitHub Desktop.
Save graphicbeacon/7921da9f0c99d15a19a6cf0a6e329f32 to your computer and use it in GitHub Desktop.
Sample code demonstrating Classes and Inheritance in Dart
void main() {
var johnny = Person('Johnny', 42)
..speak()
..name = 'Big Johnny'
..speak();
var johnnyB = Person('Johnny', 42);
print(johnny == johnnyB);
var bob = Employee('Bob', 23, DateTime.now());
bob.speak();
}
class Employee extends Person {
DateTime joinDate;
String name;
Employee(this.name, int age, this.joinDate) : super(name, age);
@override
speak() {
print('My name is $name. I joined on $joinDate.');
}
}
class Person {
var _name;
var age;
Person(name, age) {
this._name = name;
this.age = age;
}
String get name => _name;
void set name(String updatedName) => _name = updatedName;
bool operator ==(dynamic b) => _name == b._name && age == b.age;
speak() {
print("My name is $_name. I'm $age years old.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment