Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
コンストラクタ、知ってますよね?
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
okinari
January 23, 2019
Technology
130
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
コンストラクタ、知ってますよね?
okinari
January 23, 2019
More Decks by okinari
See All by okinari
AWS Lambda(+API Gateway)でバイナリを扱う話
okinari
0
120
僕の家からリモコンが消えた話(NatureRemoについて)
okinari
0
90
スマートスピーカーのすゝめ
okinari
0
71
最近のNAS製品事情
okinari
0
120
Flutterで簡単なアプリを作ってみる
okinari
0
51
Webマーケティングとは?
okinari
0
57
エンジニアにも知ってほしいWebマーケティングの世界
okinari
2
410
データ収集のすゝめ
okinari
1
330
FlutterをWEBエンジニアが扱ってみたお話
okinari
0
480
Other Decks in Technology
See All in Technology
[re:Invent2026事前勉強会(有志開催)]続きは、ラスベガスの廊下で / continue-in-the-vegas-hallway
sh_fk2
0
480
Patterns in AI Driven Software Development
akosma
PRO
0
440
新卒エンジニア、 介護士になる
ryu1013
1
670
FinOpsを導入してマルチプロダクトのコスト最適化を実現する
kworkdev
PRO
0
110
視野はAIに、視座は人に / ai-for-breadth-humans-for-perspective
nkzn
6
2.7k
スマートロックを支えるFlutter × ネイティブ連携の実践 / Flutter and Native Integration in Practice: Powering Smart Locks
bitkey
PRO
0
140
なぜAI時代に技術記事を書くのか?どう書くのか? 〜bunの場合〜
kworkdev
PRO
0
3.5k
AI時代のエンジニアの情報発信〜個人の発信を組織の力に変える〜
ryo_kawamata
0
6.6k
Building Audio Plugin Ecosystem for Android in 2026
atsushieno
1
110
20261031_IoTLT_vol140_kitazaki_v1.pdf
kitazaki
0
150
新米を「売り越さない」、Aurora DSQLで作る在庫ゼロ死守の限定ドロップEC
yama3133
0
140
SLOを阻む不安
sraku2159
0
210
Featured
See All Featured
Navigating Team Friction
lara
192
16k
Code Reviewing Like a Champion
maltzj
529
40k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.6k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
540
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
650
Accessibility Awareness
sabderemane
1
280
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
960
A better future with KSS
kneath
240
18k
30 Presentation Tips
portentint
PRO
1
450
Visualization
eitanlees
153
17k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
480
Transcript
コンストラクタ 知ってますよね?
Self-introduction Name: okinari Twitter: @okinari Job: Web Engineer(Marketing Engineer) Interested:
Dart / Flutter / Elixir / Node.js
コンストラクタとは? オブジェクト指向のプログラミング言語で 新たなオブジェクトを生成する際に呼び出されて 内容の初期化などを行なう関数あるいはメソッドのことである。 引用元:Wikipedia https://ja.wikipedia.org/wiki/コンストラクタ あんまり説明いらないですよね …
こういうのを イメージしてますよね? ※スライド中のコードは 動かないかもしれないので、 ご注意ください。 コンストラクタと言えば… class Hoge { String
bar; Hoge(String foo) { this.bar = foo; } }
これもコンストラクタ class Point { num x, y, o; Point(this.x, this.y);
Point.onlineX(this.x): this.y = 0 { print('On the X line.'); } Point.onlineY(this.y): this.x = 0 { print('On the Y line.'); } Point.origin(): this(0, 0); }
“もう笑うしかないよ
コンストラクタの種類 (多すぎるよ…) ・Default constructors ・Named constructors ・Redirecting constructors ・Constant constructors
・Factory constructors ・Initializer list(※) ※コンストラクタの種類ではない 引用元:公式のLanguage Tourより https://www.dartlang.org/guides/language/language-tour#constructors
・標準コンストラクタ ・要は普通のやつ Default constructors class Point { num x, y;
Point(num x, num y) { this.x = x; this.y = y; } }
class Point { int x, y; Point(this.x, this.y); Point.n(this.x, this.y,
int n) { print(n); } } Default constructors ・コンストラクタに限り、 引数でインスタンス変数を 変更可能 ・追加の引数はもちろんOK ・内部で処理を書くのもOK
・名前付きのコンストラクタ ・C++とかにはあるらしい? - 僕は初めて知りました ・呼び出し時、staticメソッド との見分けがつかない Named constructors class Point
{ num x, y; Point(this.x, this.y); Point.origin() { x = 0; y = 0; } }
Named constructors class Point { num x, y; Point(); Point.pConstructor()
{ this.x = 0; this.y = 0; } static Point pMethod() { Point point = new Point(); point.x = 0; point.y = 0; return point; } } void main() { // コンストラクタ Point.pConstructor(); // staticメソッド Point.pMethod(); } // どちらもPointオブジェクトが // 取得できる
・別のコンストラクタへ リダイレクト ・リダイレクトなので、 追加の処理はできない ・別のコンストラクタと、 共通の処理がある場合は、 メソッドを使う必要がある Redirecting constructors class
Point { num x, y; Point(this.x, this.y); Point.alongXAxis(num x) : this(x, 0); }
・定数コストラクタ ・final以外のフィールドは 定義不可 Constant constructors class Point { static final
Point origin = const Point(0, 0); final num x, y; const Point(this.x, this.y); }
・ファクトリーコンストラクタ ・常に新しくインスタンスを 作成するとは限らない場合、 factoryを使うべし? - ファクトリーパターン? - シングルトンパターン? ・Loggerなど…(例が思いつかん) シングルトンパターンと
ファクトリーパターンの違いに混乱 … Factory constructors class Logger { final String name; static final Map<String, Logger> _cache = <String, Logger>{}; Logger._internal(this.name); factory Logger(String name) { if (_cache.containsKey(name)) { return _cache[name]; } else { final logger = Logger._internal(name); _cache[name] = logger; return logger; } } }
Factory constructors factory Logger(String name) { if (_cache.containsKey(name)) { return
_cache[name]; } else { final logger = Logger._internal(name); _cache[name] = logger; return logger; } }
Factory constructors static Logger getInstance(String name) { if (_cache.containsKey(name)) {
return _cache[name]; } else { final logger = Logger._internal(name); _cache[name] = logger; return logger; } }
・staticメソッドで 同じように書けるような…? ・コンストラクタとの違いは (たぶん)呼び出し方だけ ・staticメソッドの場合、 メソッド名が必須なので、 名無しで表現するには、 factoryコンストラクタが必須 - 通常のコンストラクタ風
Factory constructors void main() { Logger loggerFactory = Logger('name'); Logger loggerStaticMethod = Logger.getInstance('name'); }
Initializer list ・初期化リスト? ・変数を初期化する ・右辺に同じクラスの メソッドを使う場合、 staticメソッドのみ使用可 ・finalフィールドを宣言時と コンストラクタ引数以外で 初期化できる方法
class Point { int x, y; Point(Map<String, num> json) : this.x = json['x'], this.y = json['y'] { print('call Point($x, $y);'); } }
まとめ(感想) ・コンストラクタの書き方はたくさんあるから 注意しよう! ・(Flutterの場合)オブジェクト生成にて画面を 構築するので、その簡易化が目的(かも?) ・それにしても多すぎない…?
Thanks! ご質問などあれば、 下記までお願いします。 @okinari (Twitter)