2009年7月31日金曜日

【JT】2 オブジェクト指向プログラミングの概念


* 2 オブジェクト指向プログラミングの概念
** オブジェクトとは
- 現実世界のオブジェクトのすべてに状態と振る舞いが
あるでしょ、ソフトウエアのオブジェクト指向はそれ
と同じだよ、という導入はいただけない。自然をそう
見たときそれはすでにオブジェクト指向の結果なんだ
から、結果をもって原因と言われても。。。
** クラスとは
- クラスのインスタンス = オブジェクト.
- うーん。ここの説明でクラスを理解するのは無理だ
ろう。私はCLOSをかじっているので大丈夫だが。
** 継承とは
- Javaでは、クラスの継承にて、親は常にひとつ。多
重継承は無いようだ。
- インターフェイスはクラスと外部世界との契約であ
る。この契約はビルド時にコンパイラによって強制
される。
** パッケージとは
- クラスとインターフェイスを整理するための名前空
間。
- クラスライブラリは、パッケージの集合。
- クラスライブラリはAPIとも呼ぶ。
** 設問と演習
*** 設問
- 1. 状態 振舞
- 2. フィールド
- 3. メソッド
- 4. カプセル化
- 5. クラス
- 6. スーパークラス extends サブクラス
- 7. インターフェイス
- 8. パッケージ
- 9. Application programming interface
*** 演習
- 1. こんな感じ?
---

class Radio {
int power = 0; // 0: off 1: on
int stationId = 0;
int volume = 0; // [0,10]
int volumeUnit = 1;

void togglePower() {
if (power == 0) {
power = 1;
} else {
power = 0;
}
}

void changeStationId(int newValue) {
stationId = newValue;
}

void volumeUp() {
if (volume < 11) {
volume += volumeUnit;
}
}
void voludeDown() {
if (volume > 0) {
volume -= volumeUnit;
}
}

void printStates() {
System.out.println("power:" + power +
" station id:" + stationId +
" volume:" + volume);
}
}

---

- 2. あり? エラーが出る。なんでだろ。

---
interface AVEquipment {
void volumeUp();
void volumeDown();
}

---
___

class Radio implements AVEquipment {
int power = 0; // 0: off 1: on
int stationId = 0;
int volume = 0; // [0,10]
int volumeUnit = 1;

void togglePower() {
if (power == 0) {
power = 1;
} else {
power = 0;
}
}

void changeStationId(int newValue) {
stationId = newValue;
}

void volumeUp() {
if (volume < 11) {
volume += volumeUnit;
}
}
void volumeDown() {
if (volume > 0) {
volume -= volumeUnit;
}
}

void printStates() {
System.out.println("power:" + power +
" station id:" + stationId +
" volume:" + volume);
}
}

___

こんなエラーが出る。

---

-*- mode: compilation; default-directory: "~/scratch/java/the-java-tutorial/ClassIntroduction/" -*-
Compilation started at Fri Jul 31 20:45:18

javac Radio.java
----------
1. ERROR in Radio.java (at line 19)
void volumeUp() {
^^^^^^^^^^
Cannot reduce the visibility of the inherited method from AVEquipment
----------
2. ERROR in Radio.java (at line 24)
void volumeDown() {
^^^^^^^^^^^^
Cannot reduce the visibility of the inherited method from AVEquipment
----------
2 problems (2 errors)
Compilation exited abnormally with code 255 at Fri Jul 31 20:45:20

---


こつこつ。

0 件のコメント: