Slackコミュニティに
無料で参加する
Flutterラボの
プレミアム会員になる
~Classの応用知識~ 名前付きコンストラクターの使用方法 Dart基礎【Dart】
2024.04.08
宣言方法
例として四つの辺を持つ図形を表すClassを宣言しました。
名前付きコンストラクターでallとonlyを作成し、宣言しました。allは一つの引数からすべての辺の長さを設定する仕様でonlyは4つの引数で各辺の長さを設定できる仕様にしました。
class Rectangle {
double? left;
double? top;
double? right;
double? bottom;
Rectangle.all(double value)
: left = value,
top = value,
right = value,
bottom = value;
Rectangle.only({
this.left = 0.0,
this.top = 0.0,
this.right = 0.0,
this.bottom = 0.0,
});
printRectangle() {
print('left: $left, top: $top, right: $right, bottom: $bottom');
}
}
参照方法
Rectangle square = Rectangle.all(10);
square.printRectangle();
Rectangle rectangle1 = Rectangle.only(left: 10, top: 20, right: 10, bottom: 20);
rectangle1.printRectangle();
DartPadのコード参照
void main() {
Rectangle square = Rectangle.all(10);
square.printRectangle();
Rectangle rectangle1 = Rectangle.only(left: 10, top: 20, right: 10, bottom: 20);
rectangle1.printRectangle();
}
class Rectangle {
double? left;
double? top;
double? right;
double? bottom;
Rectangle.all(double value)
: left = value,
top = value,
right = value,
bottom = value;
Rectangle.only({
this.left = 0.0,
this.top = 0.0,
this.right = 0.0,
this.bottom = 0.0,
});
printRectangle() {
print('left: $left, top: $top, right: $right, bottom: $bottom');
}
}
参考資料
初心者から始めるプログラミングスクールの紹介
我々Flutterラボは、大阪の梅田にあるコワーキングスペース『ONthe UMEDA』の料金プランとしてモバイルアプリ開発が学べるプログラミングスクールを運営しております。
オンラインではなく対面で学びたい方におすすめです。
※オンラインをご希望の方はFlutterラボのオンラインスクールをおすすめします。
以下のような方に適したプログラミングスクールです。
大阪でFlutterを学びたい方
オフラインで現役エンジニアに教わりたい方
プログラミングの基礎から学びたい方
アプリを開発してみたい方
初心者からスマホアプリをリリースしたい方
スタンダードコース
Flutter学習用のカリキュラムに合わせて、プログラミングの基礎からアプリ開発の応用まで学べるコースです。
プロコース
ご自身で開発したいアプリを、設計からリリースまですべてサポートするコースです。
ぜひ気軽にお問い合わせください。オフラインで受講ご希望の方はこちらからお問い合わせください。
15min
プレミアム会員
見放題
アプリのリリース手順を紹介
2023.03.18
¥500
【Dart】Stringからint, double, DateTimeに変換する
2020.09.14
【Dart】【Flutter】List型(リスト)の使い方とよく使うメソッドまとめ
2020.09.18
【Dart】【Flutter】DateTime型についてのまとめ
2020.10.01
【Dart】Map型の使い方とよく使うメソッドまとめ
2020.09.13