Last active
November 21, 2018 11:58
-
-
Save graphicbeacon/7921da9f0c99d15a19a6cf0a6e329f32 to your computer and use it in GitHub Desktop.
Sample code demonstrating Classes and Inheritance in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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