見出し画像

~Classの応用知識~ 名前付きコンストラクターの使用方法 Dart基礎【Dart】

宣言方法

例として四つの辺を持つ図形を表すClassを宣言しました。

名前付きコンストラクターでallonlyを作成し、宣言しました。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学習用のカリキュラムに合わせて、プログラミングの基礎からアプリ開発の応用まで学べるコースです。

プロコース

ご自身で開発したいアプリを、設計からリリースまですべてサポートするコースです。
ぜひ気軽にお問い合わせください。オフラインで受講ご希望の方はこちらからお問い合わせください。



この記事が気に入ったらサポートをしてみませんか?