Dart: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
Zeile 43: Zeile 43:
   bool get isSilent => _level == 0; // getter
   bool get isSilent => _level == 0; // getter
   set isSilent(bool value) => _level = value ? 0 : 1; // setter
   set isSilent(bool value) => _level = value ? 0 : 1; // setter
   BaseLogger(this._level);
   BaseLogger(this._level); // constructor
   BaseLogger.silent() => _level = 0;
   BaseLogger.silent() : this(0); // named constructor, "redirected constructor"
   // abstract function:
   // abstract function:
   void log(string message);
   void log(string message);
Zeile 51: Zeile 51:
     log('+++ ' + message);
     log('+++ ' + message);
   }
   }
}
class Logger extends BaseLogger {
class Logger extends BaseLogger {
   String _filename;
   String _filename;

Version vom 5. Januar 2020, 18:08 Uhr


Links

Statements

for (var ix in list) {}
for (int ix = 0; ix < 10; ix++) {}
while (doIt()){ }
do { } while(test());
switch(x) { 
  case '0':
  case 'A': y=3; break;
  case 'B': b=true; continue on_label_c; // Implementierung von "fall through"
  on_label_c: case 'C': y=5; break;
  default: break; 
}
assert(str.isEmpty(), "String muss leer sein"); // nur im Debug aktiv.

Exception

try {
  breedMoreLlamas();
} on OutOfLlamasException {
  buyMoreLlamas();
} on Exception catch (e) {
  print('Unknown exception: $e'); // Anything else that is an exception
} catch (e, stacktrace) {
  print('Something really unknown: $e $stacktrace'); // No specified type, handles all
  rethrow; // throw the same exception again
} finally {
  cleanUp();
}
throw ArgumentError('not an int');

Class

abstract class BaseLogger {
  int _errors = 0;
  int _level;
  bool get isSilent => _level == 0; // getter
  set isSilent(bool value) => _level = value ? 0 : 1; // setter
  BaseLogger(this._level); // constructor
  BaseLogger.silent() : this(0); // named constructor, "redirected constructor"
  // abstract function:
  void log(string message);
  void error(string message){
    _errors++;
    log('+++ ' + message);
  }
}
class Logger extends BaseLogger {
  String _filename;
  Logger(this._filename) : super(1);
  Logger.silentLogger(this._filename) : super.silent(0); // named constructor
}
...
  var logger = Logger('std.log');

Interface

  • Jede Klasse kann Interface sein. Dann muss jede Methode überschrieben werden
class D implements A, B, C {
  @override
  void doIt(){
    // ...
  }
}

DateTime

var date = new DateTime.now();

String

Formatierung

import 'package:sprintf/sprintf.dart';
sprintf("%02d %s", [1, "Hi"]);
print("${new DateTime.now().toString()}: $message\n");

Besonderheiten

  • entweder optionale Positionsparameter oder optionale Namensparameter, nicht beide.
String substr(String str, int pos, [int length, bool uppercase]){ ... }
<pre>String substr2(String str, int pos, {int length, bool uppercase, Logger logger}){ ... }
x = substr('Hi world', 3, 2);
y = substr2('Hi world', 3, length:2, logger:logger);
  • ..-Operator: Mehrfachzugriff auf voriges Objekt:
var person = Person()..name='Joe'..id=25;
var p = Point(10, -12)..setColor(green)..setWeight(1.22);
  • x ??= 5; // Zuweisung nur, wenn x==null
  • x ~/ 5 // Ganzzahlige Division
  • logger?.log() // Aufruf von log nur, wenn logger!=null

Reflection, Runtime-Info

a.runtimeType