class Foo {
Foo(int a, int b) {
//Code of constructor
}
Foo.named(int c, int d) {
//Code of named constructor
}
}
class Bar extends Foo {
Bar(int a, int b) : super(a, b);
}
class Baz extends Foo {
Baz(int c, int d) : super.named(c, d);
}
如果要在子类中初始化实例变量,则 super()调用必须位于初始化器列表的最后一个。
class CBar extends Foo {
int c;
CBar(int a, int b, int cParam) :
c = cParam,
super(a, b);
}
class AppException implements Exception {
final _message;
final _prefix;
//constructor with optional & not named paramters
//if not the parameter is not sent, it'll be null
AppException([this._message, this._prefix]);
String toString() {
return "$_prefix$_message";
}
}
class FetchDataException extends AppException {
//redirecting constructor with a colon to call parent two optional parameters
FetchDataException([String msg]) : super(msg, "Error During Communication: ");
}
class BadRequestException extends AppException {
BadRequestException([msg]) : super(msg, "Invalid Request: ");
}
class UnauthorisedException extends AppException {
UnauthorisedException([msg]) : super(msg, "Unauthorised: ");
}
class InvalidInputException extends AppException {
InvalidInputException([String msg]) : super(msg, "Invalid Input: ");
}