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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
110
Flutterで簡単なアプリを作ってみる
okinari
0
48
Webマーケティングとは?
okinari
0
55
エンジニアにも知ってほしいWebマーケティングの世界
okinari
2
410
データ収集のすゝめ
okinari
1
320
FlutterをWEBエンジニアが扱ってみたお話
okinari
0
480
Other Decks in Technology
See All in Technology
aws-iot-platform-architecture-use-cases.pdf
ma2shita
0
520
SREの視点で考えるSIEM活用術 〜AWS環境でのセキュリティ強化〜
cscengineer
PRO
0
230
C#コードの結合を可視化する Roslyn解析による設計改善と リファクタリング判断
dora56
0
350
SREは、MCPとAutopilotをこう使え!
kazumax55
3
950
Von-Neumann Machines, Dreams & The Futures of IT
ufried
0
120
CLIライブラリ開発を支える技術
htnabe
0
150
Genieを崇めよ
kameitomohiro
0
170
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
16
120k
Making AI Agents Safe and Fast- Jev, Obsidian, and the Meta-Harness
x5gtrn
PRO
0
110
手を動かして実感する、Kiro が変える開発体験
inariku
0
150
【技術的負債conf】事業成長に伴う技術的負債の説明責任とAIによるモニタリング、認知的負債について
i35_267
3
2.1k
AI時代の「技術的負債」の変質ー概念の終焉と再解釈、エージェントと共に向かう先
nwiizo
1
3.2k
Featured
See All Featured
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
280
The SEO identity crisis: Don't let AI make you average
varn
0
560
Odyssey Design
rkendrick25
PRO
2
810
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
940
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
Rails Girls Zürich Keynote
gr2m
96
14k
Being A Developer After 40
akosma
91
590k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
280
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Speed Design
sergeychernyshev
33
2.1k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
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)