Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cascade
- querySelector('#confirm') // Get an object.
- ..text = 'Confirm' // Use its members.
- ..classes.add('important')
- ..onClick.listen((e) => window.alert('Confirmed!'));
- class Point {
- num x, y;
- // Syntactic sugar for setting x and y
- // before the constructor body runs.
- Point(this.x, this.y);
- }
- class Point {
- num x, y;
- Point(this.x, this.y);
- // Named constructor
- Point.origin() {
- x = 0;
- y = 0;
- }
- }
- class Person {
- String firstName;
- Person.fromJson(Map data) {
- print('in Person');
- }
- }
- class Employee extends Person {
- // Person does not have a default constructor;
- // you must call super.fromJson(data).
- Employee.fromJson(Map data) : super.fromJson(data) {
- print('in Employee');
- }
- }
- import 'dart:math';
- class Point {
- final num x;
- final num y;
- final num distanceFromOrigin;
- Point(x, y)
- : x = x,
- y = y,
- distanceFromOrigin = sqrt(x * x + y * y);
- }
- // Factory
- class Logger {
- final String name;
- bool mute = false;
- // _cache is library-private, thanks to
- // the _ in front of its name.
- static final Map<String, Logger> _cache =
- <String, Logger>{};
- factory Logger(String name) {
- if (_cache.containsKey(name)) {
- return _cache[name];
- } else {
- final logger = Logger._internal(name);
- _cache[name] = logger;
- return logger;
- }
- }
- Logger._internal(this.name);
- void log(String msg) {
- if (!mute) print(msg);
- }
- }
- // Gettery settery
- class Rectangle {
- num left, top, width, height;
- Rectangle(this.left, this.top, this.width, this.height);
- // Define two calculated properties: right and bottom.
- num get right => left + width;
- set right(num value) => left = value - width;
- num get bottom => top + height;
- set bottom(num value) => top = value - height;
- }
- // A person. The implicit interface contains greet().
- class Person {
- // In the interface, but visible only in this library.
- final _name;
- // Not in the interface, since this is a constructor.
- Person(this._name);
- // In the interface.
- String greet(String who) => 'Hello, $who. I am $_name.';
- }
- // An implementation of the Person interface.
- class Impostor implements Person {
- get _name => '';
- String greet(String who) => 'Hi $who. Do you know who I am?';
- }
- String greetBob(Person person) => person.greet('Bob');
- void main() {
- print(greetBob(Person('Kathy')));
- print(greetBob(Impostor()));
- }
- // Mixins
- class Musician extends Performer with Musical {
- // ···
- }
- class Maestro extends Person
- with Musical, Aggressive, Demented {
- Maestro(String maestroName) {
- name = maestroName;
- canConduct = true;
- }
- }
- mixin Musical {
- bool canPlayPiano = false;
- bool canCompose = false;
- bool canConduct = false;
- void entertainMe() {
- if (canPlayPiano) {
- print('Playing piano');
- } else if (canConduct) {
- print('Waving hands');
- } else {
- print('Humming to self');
- }
- }
- }
- mixin MusicalPerformer on Musician {
- // ···
- }
- abstract class Cache<T> {
- T getByKey(String key);
- void setByKey(String key, T value);
- }
- class Foo<T extends SomeBaseClass> {
- // Implementation goes here...
- String toString() => "Instance of 'Foo<$T>'";
- }
- class Extender extends SomeBaseClass {...}
- // Import only foo.
- import 'package:lib1/lib1.dart' show foo;
- // Import all names EXCEPT foo.
- import 'package:lib2/lib2.dart' hide foo;
- // dart2js only!
- import 'package:greetings/hello.dart' deferred as hello;
- Future greet() async {
- await hello.loadLibrary();
- hello.printGreeting();
- }
- // You can await a loop... streams???
- await for (varOrType identifier in expression) {
- // Executes each time the stream emits a value.
- }
- // Generators
- // iterable
- Iterable<int> naturalsTo(int n) sync* {
- int k = 0;
- while (k < n) yield k++;
- }
- // Callable classes
- class WannabeFunction {
- call(String a, String b, String c) => '$a $b $c!';
- }
- main() {
- var wf = new WannabeFunction();
- var out = wf("Hi","there,","gang");
- print('$out');
- // stream which is async
- Stream<int> asynchronousNaturalsTo(int n) async* {
- int k = 0;
- while (k < n) yield k++;
- }
- // Typedefs??
- typedef Compare = int Function(Object a, Object b);
- class SortedCollection {
- Compare compare;
- SortedCollection(this.compare);
- }
- // Initial, broken implementation.
- int sort(Object a, Object b) => 0;
- void main() {
- SortedCollection coll = SortedCollection(sort);
- assert(coll.compare is Function);
- assert(coll.compare is Compare);
- }
- int sort(int a, int b) => a - b;
- void main() {
- assert(sort is Compare<int>); // True!
- }
- // Annotations
- library todo;
- class Todo {
- final String who;
- final String what;
- const Todo(this.who, this.what);
- }
- ///
- import 'todo.dart';
- @Todo('seth', 'make this do something')
- void doSomething() {
- print('do something');
- }
- // Even fucking HashCode
- class Person {
- final String firstName, lastName;
- Person(this.firstName, this.lastName);
- // Override hashCode using strategy from Effective Java,
- // Chapter 11.
- @override
- int get hashCode {
- int result = 17;
- result = 37 * result + firstName.hashCode;
- result = 37 * result + lastName.hashCode;
- return result;
- }
- // You should generally implement operator == if you
- // override hashCode.
- @override
- bool operator ==(dynamic other) {
- if (other is! Person) return false;
- Person person = other;
- return (person.firstName == firstName &&
- person.lastName == lastName);
- }
- }
- void main() {
- var p1 = Person('Bob', 'Smith');
- var p2 = Person('Bob', 'Smith');
- var p3 = 'not a person';
- assert(p1.hashCode == p2.hashCode);
- assert(p1 == p2);
- assert(p1 != p3);
- }
- message.style
- ..fontWeight = 'bold'
- ..fontSize = '3em';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement