1 / 10

第5回 iPhone アプリ開発勉強会 Objective-C 「継承とクラス」

第5回 iPhone アプリ開発勉強会 Objective-C 「継承とクラス」. 古川 拓弥. 継承とは. あるクラスの定義を引き継いで新しく定義する 継承の元となるクラス: スーパークラス (superclass) 新しく作るクラス: サブクラス (subclass) サブクラスはスーパークラスの機能をすべて利用可能 サブクラスで行うこと 新しいメソッドの追加 新しいインスタンスの追加 スーパークラスのメソッドを別の定義で置き換える. 上書き (override). method1. method1. method2. method1.

eben
Download Presentation

第5回 iPhone アプリ開発勉強会 Objective-C 「継承とクラス」

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第5回 iPhoneアプリ開発勉強会Objective-C 「継承とクラス」 • 古川拓弥

  2. 継承とは • あるクラスの定義を引き継いで新しく定義する • 継承の元となるクラス:スーパークラス(superclass) • 新しく作るクラス:サブクラス(subclass) • サブクラスはスーパークラスの機能をすべて利用可能 • サブクラスで行うこと • 新しいメソッドの追加 • 新しいインスタンスの追加 • スーパークラスのメソッドを別の定義で置き換える 上書き(override)

  3. method1 method1 method2 method1 method2 method3 method2 :新しく追加 変数 y 変数 x 変数 x 変数 x :上書き(override) 継承の概念 スーパークラス クラスA 継承 継承 サブクラス サブクラス クラスB クラスC • サブクラスではスーパークラスのインスタンス変数やメソッドにアクセス可能 • インスタンス変数,メソッドの宣言は新たに付け加えるもののみを記述

  4. method1 method3 method2 method2 method2 method1 method1 method2 method1 method3 :新しく追加 変数 y 変数 x 変数 x 変数 x 変数 x :上書き(override) 継承の概念 クラスA 継承 継承 スーパークラス クラスB クラスC 継承 サブクラス クラスBはクラスDに対してはスーパークラスとなる クラスD

  5. ルートクラス method3 method1 method2 method2 method1 method2 method1 method2 method3 method1 :新しく追加 変数 y 変数 x 変数 x 変数 x 変数 x それ以上スーパークラスを持たないクラス :上書き(override) 継承の概念 ルートクラス クラスA 継承 継承 クラスB クラスC 継承 クラスD

  6. 継承関係の宣言 • インターフェース部 • 実装部 • @interface クラス名 : スーパークラス • { • //インスタンス変数の宣言 • ・・・ • } • //メソッドの宣言 • @end すべてのクラスはルートクラスを継承しなければならない NSObject • @implementation クラス名 • //メソッドの定義 • ・・・ • @end

  7. 継承の例 • インターフェース部 • 実装部 • @interface Car : NSObject • { • char *name • } • - (void)printName; • @end 継承するクラスがないときにはNSObjectを継承する • @implementation Car • - (void)printName{ • name = “カローラ”; • printf(“車の名前は%sです\n”, name); • } • @end

  8. クラス定義とヘッダファイル • インターフェース部は1つのヘッダファイルとして作成 • #import <Foundation/NSObject.h> • @interface Alpha : NSObject • { • ・・・ • } • - (void)doSomething • @end • #import “Alpha.h” • @implementation Alpha • - (void)doSomething{ • ・・・ • } • ・・・ • @end Alpha.h Alpha.m • #import “Alpha.h” • @interface Beta : Alpha • { • ・・・ • } • ・・・ • @end • #import “Beta.h” • @implementation Beta • ・・・ • ・・・ • @end Beta.h Beta.m • #import “Beta.h” • @interface Gamma : Beta • { • ・・・ • } • ・・・ • @end • #import “Gamma.h” • @implementation Gamma • ・・・ • [self doSomething]; • ・・・ • @end Gamma.h Gamma.m

  9. self method3 method2 method1 method2 method3 method1 method1 method3 [self method1] [self method2] メッセージ処理を行っているインスタンス自身 メソッドの呼び出し • クラス内の他のメソッドの呼び出し • selfという名前にメッセージを送る [self method] 継承 継承 クラスB クラスA クラスC method2 インスタンス化 インスタンス化 メッセージ送信 メッセージ送信

  10. super method3 method2 method1 method2 method3 method1 method3 [super method1] [super method2] method1 継承した親クラスのメソッドを使うことを表す メソッドの呼び出し • スーパークラスのメソッドの呼び出し • superという名前にメッセージを送る [super method] 継承 継承 クラスB クラスA クラスC method2

More Related