0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Flutter (初級) override に関して

Posted at

親クラスで定義した機能を他のクラスに引き継がせるのに使われるoverrideですが今回は簡単な鍋料理レシピを題材にコードで流れを書いてみました。

cook.dart
// なべ料理のレシピを表すクラスを作成
class Cook {
  // 具材の名称をtoolNameとする
  final String _toolName; 
  // 下処理の工程をpreparationとする
  final String _preparation; 
  // Cookクラスのインスタンスにthisを使って入れれば使用可能な状態になる
  Cook(this._toolName, this._preparation);
  // Getterを利用してCookクラスのプライベート変数を参照できるようにする
  String get toolName => _toolName;
  String get preparation => _preparation;

  // 細かい処理のポイントを記述する為のメソッド
  List point(p) {
    return p;
  }
  // 継承させる具材の為のメソッド
 List tool(t) {
    return t;
  }
}
ingredients.dart
import 'cook.dart';
// 料理の具材を表すクラス 
class Ingredients extends Cook {
  // superで親クラス(Cook)のコンストラクタを呼び出すことができる
  Ingredients(String _toolName, String _preparation)
      : super(_toolName, _preparation);
}
soup.dart
import 'cook.dart';
// 合わせ出汁を表すクラス
class SoupStock extends Cook {
  SoupStock(String _toolName, String _preparation)
      : super(_toolName, _preparation);
}
pod.dart
import 'cook.dart';
// 完成した鍋を表すクラス
class Casserole extends Cook {
  Casserole(String _toolName, String _preparation)
      : super(_toolName, _preparation);

  // 親クラスのポイントを継承させて返す
  @override
  List point(p) {
    return ["合わせ出汁に", "下の材料を順に入れ", "一煮立ちさせる"];
  }
  // 試しにこちらも継承させてみる
   @override
  List tool(t) {
    return ["豚肉", "人参", "白菜", "小松菜"];
  }
}
main.dart
import 'cook.dart';
import 'ingredients.dart';
import 'soup.dart';
import 'pod.dart';
//メイン関数でそれぞれのメソッドを呼び出しログ(print)にだす
void main () {
  Cook pork = Ingredients("豚肉", "お酒で臭いを消す");
  print(pork.toolName);
  print(pork.preparation);
  var pointA = pork.point(["酒につけた後かるく揉み込む", "一口大にカット"]);
  print(pointA);
  print("");

  Cook vegetable = Ingredients("野菜", "しっかり洗浄する");
    print(vegetable.toolName);
  print(vegetable.preparation);
  var pointB = vegetable.point(["白菜", "小松菜", "人参", "それぞれ一口大にカット"]);
  print(pointB);
  print("");

  Cook soup = SoupStock("和出汁", "煮干しを水から煮る");
  print(soup.toolName);
  print(soup.preparation);
  var pointC = soup.point(["煮干し", "沸騰したら削り節", "削り節を投入したらすぐ漉す"]);
  print(pointC);
  print("");
  // pointD・Eでは継承されてきた内容が入ってくる
  Casserole pot = Casserole("鍋完成", "それぞれの材料を合わせて火にかける");
  print(pot.toolName);
  print(pot.preparation);
  var pointD = pot.point(p);
  print(pointD);
  var pointE = pot.tool(t);
  print(pointE);
}

ちょっと冗長になってっしまいましたが、親クラスであるCookクラスのメソッドのpoint()tool()を子クラスであるCasseroleクラスで使えているよと、伝われば。
スクリーンショット 2022-12-21 12.23.43.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?