TOP(About this memo)) > 一覧(Dart) > クラス(メンバー)
Dart is an object-oriented language with classes and mixin-based inheritance. Every object is an instance of a class, and all classes except Null descend from Object. Mixin-based inheritance means that although every class (except for the top class, Object?) has exactly one superclass, a class body can be reused in multiple class hierarchies. Extension methods are a way to add functionality to a class without changing the class or creating a subclass. Class modifiers allow you to control how libraries can subtype a class.
class A {
A(this._test, this._test2, this._test3);
int _test;
//int get _test => _test + 1; //_testという名前は既に存在するためerror(変数と衝突している?それともデフォルトで作成されるsetterと衝突?)
int get test => _test + 10;// 自分で定義したgetter
int get test2 => _test + 11;// 自分で定義したgetter
set test(int test){_test += 5;}// 自分で定義したsetter
set test2(int test){_test += 6;}// 自分で定義したsetter
final int _test2; // finalは初期化が必要
final int? _test3; // finalはnullableでも初期化が必要
//set test3(int test){_test2 = 5;}// finalなプロパティを変えることはできない。
int? x;// nullとして初期化される。
late int z;// lateはコンストラクタによる初期化は不要。
late final int l;// finalをつけた場合も同様。
int m = 0;// 0として初期化される。
}
void main() {
var a = A(5,4,3);
// var a = A(5,4); // エラー
a._test = 1;// すべてのインスタンスは暗黙的にsetterが作成される。
print(a._test);// すべてのインスタンスは暗黙的にgetterが作成される。
print(a.test);
print(a.test2);
a.test = 100000;
print(a._test);
a.test = 1000000;
print(a._test);
print(a.z); // 初期化未済のため実行時エラー
}
If you initialize a non-late instance variable where it’s declared, the value is set when the instance is created, which is before the constructor and its initializer list execute.
double speed = 5.0;
//double speed2 = speed * 2;// NG: The instance member 'speed' can't be accessed in an initializer.
late double speed2 = speed * 2;
double get speed3 => speed * 2;
Static variables aren’t initialized until they’re used.
Consider using top-level functions, instead of static methods, for common or widely used utilities and functionality.
You can use static methods as compile-time constants. For example, you can pass a static method as a parameter to a constant constructor.
void main() {
var b2 = B2(4);
print(b2._test);//
}
class B2 {
const B2(this._test);
final int _test;
}
int _privateVariable = 10;
set _privateVariable(int val) {// このような定義はできない。(既に定義されている、というエラーが生じる)
throw "do not change this field";
}
class _Page {
int _val_ = 1;// 同じライブラリ内の場合はアクセス可能。
get val => _val;
int inc() => ++_val;
void reset() => _val = 1;
}
final _page_ = _Page();
final _page_ = () {
int val = 1;// 外のスコープからはアクセスできない。
int inc() => ++val;
void reset() => val = 1;
return (val: val, inc: inc, reset: reset);
}();
main() {
final a = A(1);
print(a.f);// 1
a.f = 2;// 一般的にはfには2が代入されるように見える。
print(a.f);// 3
}
class A {
A(this._f);
int _f = 1;
int get f => _f;
set f (int fa) {
_f = fa + 1;
// ...
}
}