pi-13. Javaにおけるオブジェク
ト指向プログラミングの基本概
念と実践
1
金子邦彦
Java マスター講座:基礎から応用まで)
URL: https://www.kkaneko.jp/pro/pi/index.html
Javaを用いたオブジェクト指向プログラミングの基
概念(メソッド、クラス、継承)の理解と実践
【学習内容の構成】
1. メソッドと式の抽象化変数を使って複数の式を1
つにまとめ、オブジェクトに属する操作として定義
2. クラスとオブジェクト生成:クラス名・属性名・
データ型を指定し、コンストラクタでオブジェクト
を生成
3. スーパークラス・サブクラス・継承親クラスの属
性とメソッドを子クラスが受け継ぎ、共通機能をま
とめる
4. クラスの抽象化:類似したクラスの共通属性をスー
パークラスに集約し、プログラムのミスを減らす
前提:Javaの基本文法、Java Tutorの操作
意義:コードの再利用性向上、プログラム構造の整
によるミス削減
2
Java Tutor の起動
ウェブブラウザを起動する
Java Tutor を使いたいので,次の URL を開く
https://pythontutor.com/java.html
Java」をクリック 編集画面が開く
3
Java Tutor でのプログラム実行手
4
(1)Visualize Execution」をク
リックして実行画面に切り替える
(2)Last」をクリック.
(3) 実行結果を確認する.
(4)Edit this code」をク
リックして編集画面に戻る
Java Tutor 使用上の注意点①
実行画面で,次のような赤の表示が出ることがある
無視してよい
過去の文法ミスに関する確認表示
邪魔なときは「Close
5
Java Tutor 使用上の注意点②
please wait ... executing」のとき,10秒ほど待つ
混雑しているときは, Server Busy・・・」
というメッセージが出ることがある.
混雑している.少し(数秒から数十秒)待と自
動で表示が変わる(変わらない場合には,操作を
もう一度行ってみる)
6
13-1. メソッド
7
オブジェクトとメソッド
オブジェクト
コンピュータでの操作や処理の対象となるもののこと.
値が変化するオブジェクトのことを変数と呼んだりもする
メソッド
オブジェクトに属する操作や処理のこと
8
hero.moveDown()
hero オブジェクト
moveDown() メソッド
間を「.」で区切っている
式の抽象化
9
類似した複数の
変数 a を使って,複数
を1つにまとめる
抽象化
a * 1.1
100 * 1.1
150 * 1.1
400 * 1.1
メソッド
10
類似した複数の
変数 a を使って,複数
を1つにまとめる
抽象化
a * 1.1
100 * 1.1
150 * 1.1
400 * 1.1
a * 1.1」を含むメソッド foo 定義し使用
メソッド
11
このメソッド本体
return a * 1.1;
このメソッドは,式「 a * 1.1」に,名前 foo
を付けたものと考えることもできる
式の抽象化とメソッド
12
類似した複数の
実行結果
メソッドの定義と使用
同じ
実行結果になる
抽象化前
抽象化後
まとめ
プログラミングでのオブジェクトは,コンピュー
タでの操作や処理の対象となるもののこと
メソッドは,オブジェクトに属する操作や処理
こと
次のメソッドは,式「 a * 1.1」に,名前 foo を付
けたものと考えることもできる
式の抽象化とは,変数を使って,複数のを1つ
にまとめること
13
演習
資料:15 16
トピックス
式の抽象化
メソッド
14
15
Java Tutor のエディタで次のプログラムを入れ
実行し,結果を確認する
16
Visualize Execution」をクリック.そして「Last」をクリック.結果を確
認. Edit this code」をクリックすると,エディタの画面に戻る
13-2. クラス
17
クラス
クラスは,同じ種類のオブジェクトの集まりと考
えることができる
18
人間
学生
学生でもあり人間でもある
人間だが、学生ではない
19
円(Circle
半径 3場所(2, 4
green
半径 1,場所(8, 10
blue
円(Circle
Java のオブジェクトの生成
次の2つのオブジェクトを生成する Java プログラム
このとき,次のクラスを使うことにする
20
x 2 4 3 "green"
y 8 10 1 "blue"
クラス名 Circle
属性 x, y, r, color
x y r color
x y r color
コンストラクタ
クラス定義,コンストラクタ
Java クラス定義では,クラス名属性名と各属
性のデータ型を指定する.メソッド定義も行う.
コンストラクタとは,オブジェクトの生成を行う
メソッドのことである.
21
まとめ
Java クラス定義では,クラス名,属性名と各属
性のデータ型を指定する.
コンストラクタとは,オブジェクトの生成を行う
メソッドのことである.
キーワード
class クラス定義
new コンストラクタの呼び出し
22
演習
資料:24 25
トピックス
クラス
コンストラクタ
23
24
class Circle {
double x;
double y;
double r;
String color;
public Circle(double x, double y, double r, String color) {
this.x = x;
this.y = y;
this.r = r;
this.color = color;
}
public void printout() {
System.out.printf("%f %f %f %s¥n", this.x, this.y, this.r, this.color);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Circle x = new Circle(2, 4, 3, "green");
Circle y = new Circle(8, 10, 1, "blue");
x.printout();
y.printout();
}
}
Java Tutor のエディタで次のプログラムを入れ
実行し,結果を確認する
25
Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
Edit this code」をクリックすると,エディタの画面に戻る
メソッドと
クラス
プログラミングでのメソッド
は,オブジェクトに関する操作
や処理のこと
メソッドは,クラスに属する
メソッド内のプログラムは,そ
メソッドが所属するクラス
属性メソッドへのアクセス権
がある
26
属性やメソッド
のアクセス
「オブジェクト名」+「.
属性やメソッドアクセス
メソッド内で,そのメソッド
が所属するクラスで定義され
属性メソッドにアクセス
するときは this +「.
27
属性アクセス
28
「オブジェクト名」+「.属性やメソッドアクセスする
x 2 4 3 "green"
y 8 10 1 "blue"
x y r color
x y r color
メソッド内での属性アクセス
29
メソッド内で,そのメソッドが所属するクラスで定義され
属性メソッドにアクセスするときは this +「.
this」は,「メソッドが処理中のオブジェクトのこと
である」とみなすことも.
まとめ
メソッドは,クラスに属する
「オブジェクト名」+「.属性メソッド
クセスする
メソッド内のプログラムは,そのメソッドが所属
するクラス属性メソッドへのアクセス権があ
メソッド内で,そのメソッドが所属するクラス
定義された属性メソッドにアクセスするときは
this +「.
30
13-3. スーパークラス,サブク
ラス,継承
31
スーパークラス,サブクラス
スーパークラス図形 (Figure)
サブクラス (Circle)
(Circle)」のオブジェクトは,すべて「図形
(Figure)」である
32
図形 (Figure)
(Circle)
図形でもあり円でもある
x
y
継承の例
継承とは,スーパークラス属性メソッドサブ
クラス受け継ぐこと
次のように考える
図形 (Figure) 属性 すべて, (Circle)
される
x, y, color
(Circle) にしかない属性
r
円の属性 x, y, color, r
33
クラスの類似性
Figure Circle
属性 属性
x x
y y
color color
r
スーパークラス サブクラス
34
追加
スーパークラス,サブクラス
サブクラスは,スーパークラスの属性メソッド
べて持つ
サブクラスで,スーパークラスにない属性メソッド
追加されることがある
35
スーパークラス
サブクラス
クラス Figure
クラス Circle
属性 r を追加
クラス Figure の定義
クラス名 Figure
属性 x, y, color
36
class Figure {
double x;
double y;
String color;
public Figure(double x, double y, String color) {
this.x = x;
this.y = y;
this.color = color;
}
}
Java でのクラスの親子関係の書き方
37
親子関係の指定「class Circle extends Figure
子クラスである Circle 追加される
属性メソッドを書く
コンストラクタの定義.
super(x, y, color) により,親クラス
コンストラクタを呼び出していることに注意
スーパークラス、サブクラスのためのキーワード
38
キーワード
extends スーパークラスの指定
super スーパークラスコンストラクタ
呼び出し
演習
資料:40 42
トピックス
スーパークラス,サブクラス
継承
39
40
クラス定義
次のページに続く
クラス定義
class Figure {
double x;
double y;
String color;
public Figure(double x, double y, String color) {
this.x = x;
this.y = y;
this.color = color;
}
}
class Circle extends Figure {
double r;
public Circle(double x, double y, double r, String color) {
super(x, y, color);
this.r = r;
}
public void printout() {
System.out.printf("%f %f %f %s¥n", this.x, this.y, this.r, this.color);
}
}
Java Tutor のエディタで次のプログラムを入れ
41
次のソースコードを入れる
public class YourClassNameHere {
public static void main(String[] args) {
Circle x = new Circle(2, 4, 3, "green");
Circle y = new Circle(8, 10, 1, "blue");
x.printout();
y.printout();
}
}
実行し,結果を確認する
42
Visualize Execution」をクリック.そして「Last」をクリック.結果を確
認. Edit this code」をクリックすると,エディタの画面に戻る
まとめ
クラス階層とは,複数のクラスが親子関係をなす
こと
クラス①が,クラス②がであるとき
クラス②は,クラス①の属性メソッドすべて持つ
クラス②で,クラス①にない属性メソッド追加さ
れることがある
親子関係の指定は,「class Circle extends
Figure」のように書く.Circle が子,Figure が親.
継承とは,親クラス属性メソッド子クラス
受け継ぐこと
親クラスのことを「スーパークラス」,子クラス
のことを「サブクラス」ともいう
43
2つのクラスのプログラム
(親子関係にしない場合)
Ball Circle
44
全く同じ
r の部分
が違う
2つのクラスのプログラム
親子関係にしない場合とする場合の比較
45
親子関係しない
(同じようなプログラムを繰り返す)
親子関係にする
Ball
Circle
Ball
Circle
働きは
同じ
13-4. クラスの抽象化
46
47
円(Circle
半径 3,場所(2, 4
green
1, 高さ 2, 場所(6, 4
black
長方形
(Rectangle)
クラスの類似性
類似した2つのクラス
(Circle) 長方形 (Rectangle)
属性 属性
x x
y y
color color
r 半径 width
height 高さ
48
x, y, color
共通
クラス
クラスは,同じ種類のオブジェクトの集まりと考
えることができる
49
図形
(Circle)
円でもあり図形でもある
長方形 (Rectangle)
長方形でもあり図形でもある
クラスの抽象化
(Circle) 長方形 (Rectangle) 図形 (Figure)
属性 属性 属性
x x x
y y y
color color color
r 半径 width
height 高さ
50
共通属性を持つ
Java のオブジェクトの生成
次の2つのオブジェクトを生成する Java プログラム
51
x 2 4 "green" 3
a 6 4 "black" 1 2
x y color r
x y color width height
クラス階層は何のため?
似通ったクラス Circle, Rectangle を使いたい.プロ
グラムのミスを減らすため
将来,図形の種類を増やすときにも有効
52
Figure
Circle
Rectangle
クラス Circle, クラス Rectangle が似ている.
共通する機能を、スーパークラ Figure にまとめる.
演習
資料:54 56
【トピックス】
クラスの抽象化
53
54
次のページに続く
ここまでは
前のプログラムそのまま
class Figure {
double x;
double y;
String color;
public Figure(double x, double y, String color) {
this.x = x;
this.y = y;
this.color = color;
}
}
class Circle extends Figure {
double r;
public Circle(double x, double y, double r, String color) {
super(x, y, color);
this.r = r;
}
public void printout() {
System.out.printf("%f %f %f %s¥n", this.x, this.y, this.r, this.color);
}
}
Java Tutor のエディタで次のプログラムを入れ
55
続き
クラス定義
class Rectangle extends Figure {
double width;
double height;
public Rectangle(double x, double y, double w, double h, String color) {
super(x, y, color);
this.width = w;
this.height = h;
}
public void printout() {
System.out.printf("%f %f %f %f %s", this.x, this.y, this.width, this.height, this.color);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Circle x = new Circle(2, 4, 3, "green");
Rectangle a = new Rectangle(6, 4, 1, 2, "blue");
x.printout();
a.printout();
}
}
実行し,結果を確認する
56
Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
Edit this code」をクリックすると,エディタの画面に戻る
13-5. Java プログラム例
57
配列と繰り返し
58
配列の
組み立て
y[i] = x[i] * 1.1」を
i の値を変えながら
5回繰り返す
演習
資料:60 62
トピックス
配列
繰り返し
59
60
Java Tutor のエディタで次のプログラムを入れ,
実行し,結果を確認する
public class YourClassNameHere {
public static void main(String[] args) {
double x[] = {8, 6, 4, 2, 3};
double y[] = {0, 0, 0, 0, 0};
int i;
for(i=0; i<=4; i++) {
y[i] = x[i] * 1.1;
}
for(i=0; i<=4; i++) {
System.out.println(y[i]);
}
}
}
import java.util.Random;
public class YourClassNameHere {
public static void main(String[] args) {
Random r = new Random();
int i, a;
for(i=0; i<10; i++) {
a = r.nextInt(100);
System.out.println(a);
}
}
}
疑似乱数を10個作る
61
標準ライブラリ
java.util.Random のインポート
疑似乱数の生成と
表示を10回
繰り返し
62
表示を確認
0 から 99 の乱数が 10
表示される.
Java Tutor のエディタで次のプログラムを入れ,
実行し,結果を確認する
import java.util.Random;
public class YourClassNameHere {
public static void main(String[] args) {
Random r = new Random();
int i, a;
for(i=0; i<10; i++) {
a = r.nextInt(100);
System.out.println(a);
}
}
}
関連ページ
Java プログラミング入門
GDB online を使用
https://www.kkaneko.jp/pro/ji/index.html
Java の基本
Java Tutor, GDB online を使用
https://www.kkaneko.jp/pro/pi/index.html
Java プログラム例
https://www.kkaneko.jp/pro/java/index.html
63
13-1
public class YourClassNameHere {
public static double foo(double a) {
return a * 1.1;
}
public static void main(String[] args) {
System.out.printf("%f¥n", foo(100));
System.out.printf("%f¥n", foo(150));
System.out.printf("%f¥n", foo(400));
}
}
64
13-2
65
class Circle {
double x;
double y;
double r;
String color;
public Circle(double x, double y, double r, String color) {
this.x = x;
this.y = y;
this.r = r;
this.color = color;
}
public void printout() {
System.out.printf("%f %f %f %s¥n", this.x, this.y, this.r, this.color);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Circle x = new Circle(2, 4, 3, "green");
Circle y = new Circle(8, 10, 1, "blue");
x.printout();
y.printout();
}
}
13-3
class Figure {
double x;
double y;
String color;
public Figure(double x, double y, String color) {
this.x = x;
this.y = y;
this.color = color;
}
}
class Circle extends Figure {
double r;
public Circle(double x, double y, double r, String color) {
super(x, y, color);
this.r = r;
}
public void printout() {
System.out.printf("%f %f %f %s¥n", this.x, this.y, this.r, this.color);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Circle x = new Circle(2, 4, 3, "green");
Circle y = new Circle(8, 10, 1, "blue");
x.printout();
y.printout();
}
}
66
13-4
class Figure {
double x;
double y;
String color;
public Figure(double x, double y, String color) {
this.x = x;
this.y = y;
this.color = color;
}
}
class Circle extends Figure {
double r;
public Circle(double x, double y, double r, String color) {
super(x, y, color);
this.r = r;
}
public void printout() {
System.out.printf("%f %f %f %s¥n", this.x, this.y, this.r, this.color);
}
}
class Rectangle extends Figure {
double width;
double height;
public Rectangle(double x, double y, double w, double h, String color) {
super(x, y, color);
this.width = w;
this.height = h;
}
public void printout() {
System.out.printf("%f %f %f %f %s", this.x, this.y, this.width, this.height, this.color);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Circle x = new Circle(2, 4, 3, "green");
Rectangle a = new Rectangle(6, 4, 1, 2, "blue");
x.printout();
a.printout();
}
}
67
13-5
public class YourClassNameHere {
public static void main(String[] args) {
double x[] = {8, 6, 4, 2, 3};
double y[] = {0, 0, 0, 0, 0};
int i;
for(i=0; i<=4; i++) {
y[i] = x[i] * 1.1;
}
for(i=0; i<=4; i++) {
System.out.println(y[i]);
}
}
}
68
13-5 2つめ
import java.util.Random;
public class YourClassNameHere {
public static void main(String[] args) {
Random r = new Random();
int i, a;
for(i=0; i<10; i++) {
a = r.nextInt(100);
System.out.println(a);
}
}
}
69