pi-8. Javaにおけるクラス設計
1
金子邦彦
(Java マスター講座:基礎から応用まで)
URL: https://www.kkaneko.jp/pro/pi/index.html
アウトライン
2
番号
項目
復習
8
-1
クラスの設計の例
8
-2
オブジェクトの状態と状態変化
8
-3
演習
8
-4
メソッド内でのみ使用する変数
8
-5
抽象化の組み合わせ
各自、資料を読み返したり、課題に取り組んだりも行う
この授業では、Java を用いて基礎を学び、マスターする
クラスとオブジェクト
クラスは,同じ種類のオブジェクトの集まりと考え
ることができる
3
人間
学生
学生でもあり人間でもある
人間だが,学生ではない
4
半径 3,場所(2, 4)
色 green
半径 1,場所(8, 10)
色 blue
クラス定義のプログラム
クラス Ball
オブジェクト
オブジェクト
オブジェクト生成のプログラム
メソッドアクセスのプログラム
Ball a = new Ball(8, 10, 1, "blue");
Ball b = new Ball(2, 4, 3, "green");
a.printout();
b.printout();
Java のクラス
クラス定義のプログラム
5
クラス名: Ball
メソッド: Ball
メソッド: printout
このクラス定義を使用した,オブジェクトの生成
a 8 10 1 "red"
b 2 4 3 "green"
x y r color
Javaプログラム
Ball a = new Ball(8, 10, 1, "blue");
Ball b = new Ball(2, 4, 3, "green");
4つの属性
メソッドアクセス
6
a や b オブジェクト
printout() メソッド
間を「.」で区切っている
Javaプログラム
a.printout();
b.printout();
Java Tutor の起動
① ウェブブラウザを起動する
② Java Tutor を使いたいので,次の URL を開く
https://pythontutor.com/java.html
③ 「Java」をクリック ⇒ 編集画面が開く
7
Java Tutor でのプログラム実行手順
8
(1)「Visualize Execution」をク
リックして実行画面に切り替える
(2)「Last」をクリック.
(3) 実行結果を確認する.
(4)「Edit this code」をク
リックして編集画面に戻る
Java Tutor 使用上の注意点①
• 実行画面で,次のような赤の表示が出ることがある →
無視してよい
過去の文法ミスに関する確認表示
邪魔なときは「Close」
9
Java Tutor 使用上の注意点②
「please wait ... executing」のとき,10秒ほど待つ.
→ 混雑しているときは, 「Server Busy・・・」
というメッセージが出ることがある.
混雑している.少し(数秒から数十秒)待つと自
動で表示が変わる(変わらない場合には,操作を
もう一度行ってみる)
10
8-1. クラス設計の例
11
クラスの設計例 ①
1. クラス名は Signal
2. 属性 color は文字列である.
3. 属性 color は “red”, “yellow”, “blue” の 3通り.
class Signal {
String color;
public Signal(String color) {
this.color = color;
}
}
public class YourClassNameHere {
public static void main(String [] args) {
Signal s = new Signal("red");
}
}
12
クラス定義
クラスの設計例 ①
1. クラス名は Signal
2. 属性 color は文字列である.
3. 属性 color は “red”, “yellow”, “blue” の 3通り.
class Signal {
String color;
public Signal(String color) {
this.color = color;
}
}
public class YourClassNameHere {
public static void main(String [] args) {
Signal s = new Signal("rrd");
}
}
13
クラス定義
書き間違い
書き間違ってしまっても、
実行できてしまうので、
もろいプログラム
あとで書き間違いを探すのは面倒そう
演習
資料:13 ~ 15
【トピックス】
• クラス定義
• class
• オブジェクト生成(コンスト
ラクタ)
14
① Java Tutor のエディタで次のプログラムを入れる
15
16
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
② 実行し,結果を確認する.
③ 「red」を「rrd」と書き換えても実行できる
(あとで使うので消さないこと)
17
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
クラスの設計例 ②
1. クラス名は Signal
2. 属性 color は文字列である.
3. 属性 color は “red”, “yellow”, “blue” の 3通り.
class Signal {
String color;
public Signal() {};
public void red() { this.color = "red"; };
public void yellow() { this.color = "yellow"; };
public void blue() { this.color = "blue"; };
}
public class YourClassNameHere {
public static void main(String [] args) {
Signal s = new Signal();
s.red();
}
}
18
新しいクラス定義
・コンストラクタでは、
何も行わない
・メソッド red, yellow, blue
を新設
・「void」は、「メソッド
の返り値なし」の意味
演習
資料:18 ~ 19
【トピックス】
• クラス設計
19
20
① Java Tutor のエディタで次のプログラムを入れる
21
② 実行し,結果を確認する
(あとで使うので消さないこと)
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
まとめ
2つの方法の比較
22
プログラムの長さ: ①短い、②長い
不正なデータの混入可能性: ①あり、②なし
クラス ①
クラス ②
8-2. オブジェクトの状態と
状態変化
23
オブジェクトの状態と状態変化
信号の状態を、属性 color で扱う
• 属性 color は “red”, “yellow”, “blue” の 3通り.
• 属性 color は、次のように変化する
24
“blue”
“Yellow”
“red”
状態変化のメソッドの例
class Signal {
String color;
public Signal() {};
public void red() { this.color = "red"; };
public void yellow() { this.color = "yellow"; };
public void blue() { this.color = "blue"; };
public void go() {
if (this.color.equals("blue")) { this.yellow(); }
else if (this.color.equals("yellow")) { this.red(); }
else if (this.color.equals("red")) { this.blue(); }
}
}
※ equals メソッドは文字列の比較
25
演習
資料:25 ~ 26
【トピックス】
• オブジェクトの状態変化
26
① Java Tutor のエディタで次のプログラムを入れる
27
② 実行し,結果を確認する
28
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
8-3. 演習
29
オブジェクトの状態と状態変化
製品の状態を、属性 s で扱う
• 属性 s は “active”, “deactive” の 2通り.
• 属性 s は、次のように変化する
30
“active”
“deactive”
メソッド on
メソッド off
ソースコード
class Product {
String s;
public Product() {};
public void active() { this.s = "active"; };
public void deactive() { this.s = "deactive"; };
public void on() {
if (this.s.equals("deactive")) { this.active(); }
}
public void off() {
if (this.s.equals("active")) { this.deactive(); }
}
}
public class YourClassNameHere {
public static void main(String [] args) {
Product p = new Product();
p.deactive();
p.on();
}
}
31
演習
資料:31 ~ 32
【トピックス】
• オブジェクトの状態と状態変
化
32
① Java Tutor のエディタで次のプログラムを入れる
33
② 実行し,結果を確認する
34
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
8-4. メソッド内でのみ
使用する変数
35
• メソッド foo : 変数 a を使用
• メソッド main: 変数 p を使用
36
演習
資料:36 ~ 37
【トピックス】
• メソッド内で使用される変数
は,メソッドの実行終了とと
もに,自動で消える
37
Java Tutor のエディタで次のプログラムを入れ,実
行し,結果を確認する(あとで使うので消さないこ
と)
38
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
• 変数 p は、main の中でのみ利用できる.foo の中
では利用できないから、次のプログラムは動かない
39
8-5. 抽象化の組み合わせ
40
式の抽象化
41
類似した複数の式
a * 100 * 1.1
5 * 100 * 1.1
12 * 100 * 1.1
変数を使って,複数の
式を1つにまとめる
(抽象化)
抽象化の組み合わせ
42
類似した複数の式
a * 1.1
5 * 100 * 1.1
12 * 100 * 1.1
8 * 200 * 1.1
16 * 200 * 1.1
抽象化
b * 100 * 1.1
c * 200 * 1.1
抽象化
43
a * 1.1
5 * 100 * 1.1
12 * 100 * 1.1
8 * 200 * 1.1
16 * 200 * 1.1
抽象化
b * 100 * 1.1
c * 200 * 1.1
抽象化
演習
資料:43 ~ 44
【トピックス】
• 抽象化
44
① Java Tutor のエディタで次のプログラムを入れる
45
② 実行し,結果を確認する
46
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
8-2. のプログラム
class Signal {
String color;
public Signal() {};
public void red() { this.color = "red"; };
public void yellow() { this.color = "yellow"; };
public void blue() { this.color = "blue"; };
public void go() {
if (this.color.equals("blue")) { this.yellow(); }
else if (this.color.equals("yellow")) { this.red(); }
else if (this.color.equals("red")) { this.blue(); }
}
}
public class YourClassNameHere {
public static void main(String [] args) {
Signal s = new Signal();
s.red();
s.go();
}
}
47
8-3. のプログラム
class Product {
String s;
public Product() {};
public void active() { this.s = "active"; };
public void deactive() { this.s = "deactive"; };
public void on() {
if (this.s.equals("deactive")) { this.active(); }
}
public void off() {
if (this.s.equals("active")) { this.deactive(); }
}
}
public class YourClassNameHere {
public static void main(String [] args) {
Product p = new Product();
p.deactive();
p.on();
}
}
48
8-4. のプログラム
public class YourClassNameHere {
public static double foo(double a) {
return a * 1.1;
}
public static void main(String [] args) {
double p;
p = 120;
System.out.printf("%f¥n", foo(p));
p = 200;
System.out.printf("%f¥n", foo(p));
}
}
49
8-5 のプログラム
public class YourClassNameHere {
public static double x(double a) {
return a * 1.1;
}
public static double y(double b) {
return x(b * 100);
}
public static double z(double c) {
return x(c * 200);
}
public static void main(String [] args) {
System.out.printf("%f¥n", y(5));
System.out.printf("%f¥n", y(12));
System.out.printf("%f¥n", z(8));
System.out.printf("%f¥n", z(16));
}
}
50
全体まとめ
51
• オブジェクトの属性に、不正なデータが入らない
ように、クラスの設計、メソッドを工夫できる
• オブジェクトの属性とメソッドを、状態、状態変
化のようにとらえることができる場合がある
• 抽象化にはバリエーションがある。(新しい種類
の抽象化を説明します)
関連ページ
• 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
52