14 p Person Teacher public class Teacher extends
![](https://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-1.jpg)
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-2.jpg)
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-3.jpg)
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-4.jpg)
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-5.jpg)
![多相性の例(1/4) p Personの子ク ラス(Teacher クラス)をもう一 つ作成. public class Teacher extends Person { String title; 多相性の例(1/4) p Personの子ク ラス(Teacher クラス)をもう一 つ作成. public class Teacher extends Person { String title;](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-6.jpg)
多相性の例(1/4) p Personの子ク ラス(Teacher クラス)をもう一 つ作成. public class Teacher extends Person { String title; // 役職 String get. Title() { return title; } void set. Title(String t) { title = t; } } Person クラスを 拡張して 作成する
![多相性の例(2/4) Personにメソッ ドを追加. p Studentおよび Teacherで そのメソッドを オーバーライド public class Person { : void 多相性の例(2/4) Personにメソッ ドを追加. p Studentおよび Teacherで そのメソッドを オーバーライド public class Person { : void](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-7.jpg)
多相性の例(2/4) Personにメソッ ドを追加. p Studentおよび Teacherで そのメソッドを オーバーライド public class Person { : void go. To. Lecture(String lecture) { System. out. println(lecture + "を受講できない "); } } public class Student extends Person { : void go. To. Lecture(String lecture) { System. out. println(lecture + "を受講する"); } } public class Teacher extends Person { : void go. To. Lecture(String lecture) { System. out. println(lecture + "を講義する"); } }
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-8.jpg)
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-9.jpg)
![多相性の利点(1/3) p 異なる型のイン スタンスをまと めて処理でき る. public static void main(String[] args) { Person p[] 多相性の利点(1/3) p 異なる型のイン スタンスをまと めて処理でき る. public static void main(String[] args) { Person p[]](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-10.jpg)
多相性の利点(1/3) p 異なる型のイン スタンスをまと めて処理でき る. public static void main(String[] args) { Person p[] = new Person[3]; p[0] = new Person(); Person型の配列 p[1] = new Student(); を宣言 p[2] = new Teacher(); go. To. Lecture. Together(p, "オブジェクト指向プログラミング"); } public static void go. To. Lecture. Together( Person p[], String lecture) { for (int n = 0; n < p. length; n++) { p[n]. go. To. Lecture(lecture); } } 同じ実行文で異なる型を処理できる (見た目の型はPerson)
![多相性の利点(2/3) p 新しい子クラス を作成して拡 張してみると… public class Staff extends Person { void go. To. 多相性の利点(2/3) p 新しい子クラス を作成して拡 張してみると… public class Staff extends Person { void go. To.](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-11.jpg)
多相性の利点(2/3) p 新しい子クラス を作成して拡 張してみると… public class Staff extends Person { void go. To. Lecture(String lecture) { System. out. println(lecture + "を見学する"); } } オーバーライド
![多相性の利点(3/3) p 新しいクラスを 追加してもプロ グラムの書き 換えを最小限 にできる. プログラムの書き 換えは不要! public static void main(String[] args) 多相性の利点(3/3) p 新しいクラスを 追加してもプロ グラムの書き 換えを最小限 にできる. プログラムの書き 換えは不要! public static void main(String[] args)](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-12.jpg)
多相性の利点(3/3) p 新しいクラスを 追加してもプロ グラムの書き 換えを最小限 にできる. プログラムの書き 換えは不要! public static void main(String[] args) { Person p[] = new Person[4]; p[0] = new Person(); p[1] = new Student(); 新しいクラスの p[2] = new Teacher(); インスタンス p[3] = new Staff(); go. To. Lecture. Together(p, "オブジェクト指向プログラミング"); } public static void go. To. Lecture. Together( Person p[], String lecture) { for (int n = 0; n < p. length; n++) { p[n]. go. To. Lecture(lecture); } }
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-13.jpg)
![](http://slidetodoc.com/presentation_image_h/16d0dbf76de99004b179222d4cdcc7e2/image-14.jpg)
- Slides: 14