Java p extends n n class Vector 3

  • Slides: 9
Download presentation

Javaによるオブジェクト指向 (継承) p extends キーワードを使って継承する. n n 拡張するメンバ変数,メソッドのみ定義する. 例: class Vector 3 D extends

Javaによるオブジェクト指向 (継承) p extends キーワードを使って継承する. n n 拡張するメンバ変数,メソッドのみ定義する. 例: class Vector 3 D 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; メソッド } Vector 3 D outer_product(Vector 3 D a) { return new Vector 3 D(y*a. z - z*a. y, z*a. x - x*a. z, x*a. y - y*a. x); } }

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

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

Javaによるオブジェクト指向 (継承とメソッド呼出し) p メソッド呼出しの例: class Test { public static void main(String args[]) { Vector

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

Javaによるオブジェクト指向 (多態性 1) p ところで… class Vector { : double length() { return Math.

Javaによるオブジェクト指向 (多態性 1) p ところで… class Vector { : double length() { return Math. sqrt(this. inner_product(this)); } どちらの inner_product } が呼ばれるか? class Test { public static void main(String args[]) { Vector a = new Vector(5, 10); : Vector 3 D c = new Vector 3 D(5, 10, 15); : System. out. println(a. length()); Vector の length() System. out. println(c. length()); Vector の length() :

Javaによるオブジェクト指向 (多態性 2) p 多態性(ポリモルフィズム): class Vector { this の「実際の」型に応じて : 呼び分けられる double length()

Javaによるオブジェクト指向 (多態性 2) p 多態性(ポリモルフィズム): class Vector { this の「実際の」型に応じて : 呼び分けられる double length() { return Math. sqrt(this. inner_product(this)); } this = c } this = a class Test { public static void main(String args[]) { Vector a = new Vector(5, 10); : Vector 3 D c = new Vector 3 D(5, 10, 15); : System. out. println(a. length()); System. out. println(c. length()); :

Javaによるオブジェクト指向 (多態性 3) p ポリモルフィズムの別の例: class Test { public static void main(String args[]) {

Javaによるオブジェクト指向 (多態性 3) p ポリモルフィズムの別の例: class Test { public static void main(String args[]) { Vector a = new Vector(5, 10); Vector b = new Vector(15, 20); System. out. println(a. inner_product(b)); Vector c = new Vector 3 D(5, 10, 15); Vector d = new Vector 3 D(20, 25, 30); System. out. println(c. inner_product(d)); c = a; cはVectorで宣言されているが, d = b; 「実際の」型はVector 3 D System. out. println(a. inner_product(b)); } cはVectorで宣言されているが, } 「実際の」型はVector