Presentation is loading. Please wait.

Presentation is loading. Please wait.

オブジェクト指向言語論 第六回 知能情報学部 新田直也.

Similar presentations


Presentation on theme: "オブジェクト指向言語論 第六回 知能情報学部 新田直也."— Presentation transcript:

1 オブジェクト指向言語論 第六回 知能情報学部 新田直也

2 オブジェクト指向の考え方 プログラムをオブジェクトの塊と見る. C言語: プログラムを関数(手続き)の塊と見る.
関数型言語: プログラムを(数学的な)関数の塊と見る. 人工知能型言語: プログラムを知識(論理式)の塊と見る. オブジェクト メッセージ

3 オブジェクトとメッセージ オブジェクト同士はメッセージで互いに情報をやり取りする. オブジェクトはお互いに相手の中身を知らない.
オブジェクトの型をクラスという. クラス

4 Javaによるオブジェクト指向 (クラス)
クラスは,インスタンス変数とメソッドをカプセル化したもの. 例: class Vector { double x; double y; Vector add(Vector a) { x += a.x; y += a.y; return this; } double inner_product(Vector a) { return x * a.x + y * a.y; } double length() { return Math.sqrt(inner_product(this)); } } インスタンス変数 メソッド

5 Javaによるオブジェクト指向 (インスタンス)
オブジェクトはインスタンスとして表現される. インスタンスは new によって生成される. 構文: new クラス名() 例: class Test { public static void main(String args[]) { Vector a, b; a = new Vector(); b = new Vector(); } } 参照型変数(後で説明) Vectorのインスタンスの生成

6 Javaによるオブジェクト指向 (メッセージ)
メッセージの送信はメソッド呼出しに対応する. 構文: インスタンス名.メソッド名(実引数1, 実引数2, …) 例: class Test { public static void main(String args[]) { Vector a, b; double d; a = new Vector(); b = new Vector(); a.x = 5; a.y = 10; b.x = 15; b.y = 20; d = a.inner_product(b); System.out.println(d); } } a の inner_product() を呼出し

7 Javaによるオブジェクト指向 (コンストラクタ)
インスタンスの生成も実はメソッド呼出し. コンストラクタ: インスタンスを生成するメソッド(引数を持てる). 例: class Vector { : Vector(double px, double py) { x = px; y = py; } : } class Test { public static void main(String args[]) { Vector a, b; double d; a = new Vector(5, 10); b = new Vector(15, 20); : クラスと 同一名 コンストラクタ

8 Javaによるオブジェクト指向 (変数宣言)
変数には基本型と参照型がある. 基本型: int, double, float, long, boolean 参照型: 配列型,クラス型 宣言は使用前ならどこでも可能 class Test { public static void main(String args[]) { Vector a = new Vector(5, 10); Vector b = a; double d = a.length(); System.out.println(d); b.x = 10; d = a.length(); System.out.println(d); } } Vector a b 参照型 a の内容を b の内容に コピーするわけではない. b が a と同じオブジェクト を指すようになる. 基本型 a.x の値も同時に変わる.

9 Javaによるオブジェクト指向 (this演算子)
例: class Vector { double x; double y; Vector add(Vector a) { this.x += a.x; this.y += a.y; return this; } double inner_product(Vector a) { return this.x * a.x + this.y * a.y; } double length() { return Math.sqrt(this.inner_product(this)); } } class Vector { double x; double y; Vector add(Vector a) { x += a.x; y += a.y; return this; } double inner_product(Vector a) { return x * a.x + y * a.y; } double length() { return Math.sqrt(inner_product(this)); } }

10 継承 継承: 基本となるクラスを拡張して,新しいクラスを定義すること. 親クラス: 元となるクラス.スーパークラスとも言う.
親クラス: 元となるクラス.スーパークラスとも言う. 子クラス: 親クラスを拡張したクラス.拡張した部分だけ定義する. クラスA 基底クラス クラスC 拡張 クラスB 拡張 クラスD 拡張

11 Javaによるオブジェクト指向 (継承) extends キーワードを使って継承する. 拡張するインスタンス変数,メソッドのみ定義する.
例: class Vector3D extends Vector { double z; Vector add(Vector a) { x += a.x; y += a.y; z += a.z; return this; } double inner_product(Vector a) { return x * a.x + y * a.y + z * a.z; } Vector3D outer_product(Vector3D a) { return new Vector3D(y*a.z - z*a.y, z*a.x - x*a.z, x*a.y - y*a.x); } } 拡張するメンバ変数 置き換える メソッド 拡張する メソッド

12 Javaによるオブジェクト指向 (オーバーライド)
オーバーライド: 継承によるメソッドの置き換え. Vector3Dでは,addと inner_productをオーバーライドしている. Vector double x; double y; Vector add(Vector a); double inner_product(Vector a); double length(); Vector3D double z; Vector add(Vector a); double inner_product(Vector a); Vector3D outer_product(Vector3D a);

13 Javaによるオブジェクト指向 (継承とメソッド呼出し)
メソッド呼出しの例: →変数の型宣言だけを見て呼出し先がわかることに注意!! class Test { public static void main(String args[]) { Vector a = new Vector(5, 10); Vector b = new Vector(15, 20); Vector3D c = new Vector3D(5, 10, 15); Vector3D d = new Vector3D(20, 25, 30); System.out.println(a.inner_product(b)); System.out.println(c.inner_product(d)); System.out.println(a.length()); System.out.println(c.length()); a = a.outer_product(b); c = c.outer_product(d); } } Vectorの Vector3Dの Vectorの Vectorの エラー!! Vector3Dの


Download ppt "オブジェクト指向言語論 第六回 知能情報学部 新田直也."

Similar presentations


Ads by Google