Contenu connexe Similaire à Ssaw08 0930 (20) Plus de Atsushi Tadokoro (20) Ssaw08 09303. OOP理解のポイント
• OOP = 難しい?
• OOPの難しげな用語たち
• オブジェクト
• メソッド
• メッセージ
• 継承
• 抽象化
• カプセル化
• ポリモーフィズム (多態性)
• クラス
• インスタンス化
• 言葉だけだとかなり難しそう…
• 本質を理解すると、実はやっていることは単純
5. プログラミング・パラダイムの変遷
実例:COBOLによるHello World
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. HELLOWORLD.
000300 DATE-WRITTEN. 02/05/96 21:04.
000400* AUTHOR BRIAN COLLINS
000500 ENVIRONMENT DIVISION.
000600 CONFIGURATION SECTION.
000700 SOURCE-COMPUTER. RM-COBOL.
000800 OBJECT-COMPUTER. RM-COBOL.
000900
001000 DATA DIVISION.
001100 FILE SECTION.
001200
100000 PROCEDURE DIVISION.
100100
100200 MAIN-LOGIC SECTION.
100300 BEGIN.
100400 DISPLAY " " LINE 1 POSITION 1 ERASE EOS.
100500 DISPLAY "HELLO, WORLD." LINE 15 POSITION 10.
100600 STOP RUN.
100700 MAIN-LOGIC-EXIT.
100800 EXIT.
13. • オブジェクト指向プログラムのポイント:その4
• 必要のない情報は隠す (カプセル化)
• プログラムの実装全てを知る必要はない
• 必要なインターフェイス(接点)だけ見せて、あとは隠す
オブジェクト指向プログラミングの概念
To invent programs, you need to be able to capture abstractions and ex
design. It’s the job of a programming language to help you do this. The
process of invention and design by letting you encode abstractions tha
It should let you make your ideas concrete in the code you write. Surf
the architecture of your program.
All programming languages provide devices that help express abstrac
are ways of grouping implementation details, hiding them, and giving
a common interface—much as a mechanical object separates its interfa
illustrated in “Interface and Implementation” .
Figure 2-1 Interface and Implementation
9
10
11
8
7
6
implementationinterface
インターフェイス 実装
15. オブジェクト指向プログラミングの概念
• クラス
• クラスとは:オブジェクトの「型紙」
• クラスをインスタンス化 (実体化) することでインスタンスオブジェクト
となる
色
重さ(g)
味
リンゴ
(クラス)
実
が
な
る
成
長
す
る
落
ち
る
腐
る
赤
5.0
甘い
ふじ
(インスタンスオブジェクト)
実
が
な
る
成
長
す
る
落
ち
る
腐
る
青
4.0
すっぱい
青リンゴ
(インスタンスオブジェクト)
実
が
な
る
成
長
す
る
落
ち
る
腐
る
インスタンス化
18. 丸を描く (クラスを使わない例)
int x = 150;
int y = 200;
int diameter = 30;
void setup() {
size(400, 400);
smooth();
noStroke();
}
void draw() {
background(0);
ellipse(x, y, diameter, diameter);
}
22. 丸を描く
クラスを宣言、属性を定義
Spot sp; // クラスを宣言
void setup() {
size(400, 400);
smooth();
noStroke();
sp = new Spot(); // クラスの生成 (インスタンス化)
sp.x = 150; // クラスの属性「x」に150を代入
sp.y = 200; // クラスの属性「y」に200を代入
sp.diameter = 30; // クラスの属性「diameter」に30を代入
}
void draw() {
background(0);
ellipse(sp.x, sp.y, sp.diameter, sp.diameter);
}
26. 丸を描く
メソッドを定義
Spot sp;
void setup() {
size(100, 100);
smooth();
noStroke();
sp = new Spot();
sp.x = 33;
sp.y = 50;
sp.diameter = 30;
}
void draw() {
background(0);
sp.display();
}
28. 丸を描く
コンストラクタを定義
• コンストラクタ (構築子、Constructor) とは
• オブジェクトをインスタンス化する際に呼び出されて、内容の初期化な
どを行う関数
• クラス名と同一の名前を持つメソッドとして定義される
• 戻り値は持たない
• コンストラクタに渡す引数により初期化のバリエーションが定義される
• 例:Spotクラスのコンストラクタ
• Spot( 引数1, 引数2, 引数3 ...) { }
• Spotクラスの引数に以下の3つのパラメータを渡せるようにする
• float x;
• float y;
• float diameter;
31. 丸を描く
コンストラクタを定義
class Spot {
float x, y, diameter;
Spot(float x, float y, float diameter) {
//クラスのインスタンス x(this.x) に、引数の値xを代入
this.x = x;
//クラスのインスタンス y(this.y) に、引数の値yを代入
this.y = y;
//diameter(this.diameter) に、引数の値diameterを代入
this.diameter = diameter;
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
34. 丸を動かしてみる
Spot sp; //オブジェクトの宣言
void setup() {
size(400, 400);
smooth();
noStroke();
sp = new Spot(150, 200, 30, 5); //インスタンス化
}
void draw() {
fill(0, 15);
rect(0, 0, width, height);
fill(255);
sp.move(); //丸の座標を更新
sp.display(); //丸を表示
}
35. 丸を動かしてみる
class Spot {
float x, y;
float diameter;
float speed; //移動スピード
int direction = 1; //移動の向き (1 下向き, -1 上向き)
Spot(float xpos, float ypos, float dia, float sp) {
x = xpos;
y = ypos;
diameter = dia;
speed = sp;
}
void move() { //上下の端に到達した際に向きが反転する
y += (speed * direction);
if ((y > (height - diameter / 2)) || (y < diameter / 2)) {
direction *= -1;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
38. 複数のオブジェクトを配置する
Spot sp1, sp2, sp3; //3つのオブジェクトを宣言
void setup() {
size(400, 400);
smooth();
noStroke();
sp1 = new Spot(100, 200, 80, 2); //sp1を生成
sp2 = new Spot(200, 200, 20, 5); //sp2を生成
sp3 = new Spot(300, 50, 60, 3); //sp3を生成
}
void draw() {
fill(0, 15);
rect(0, 0, width, height);
fill(255);
sp1.move();
sp2.move();
sp3.move();
sp1.display();
sp2.display();
sp3.display();
}
39. 複数のオブジェクトを配置する
class Spot {
float x, y;
float diameter;
float speed;
int direction = 1;
Spot(float xpos, float ypos, float dia, float sp) {
x = xpos;
y = ypos;
diameter = dia;
speed = sp;
}
void move() {
y += (speed * direction);
if ((y > (height - diameter / 2)) || (y < diameter / 2)) {
direction *= -1;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
44. Particleクラス
class Particle {
float x, y;
float vx, vy;
float radius;
float gravity = 0.1;
Particle(int x, int y, float vx, float vy, float radius) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = radius;
}
void update() {
vy = vy + gravity;
y += vy;
x += vx;
}
void display() {
ellipse(x, y, radius*2, radius*2);
}
}
45. Particleを使用する
Particle p;
void setup() {
size(400, 400);
noStroke();
smooth();
p = new Particle(0, height, 2.5, -8, 10.0);
}
void draw() {
fill(0, 10);
rect(0, 0, width, height);
fill(255);
p.update();
p.display();
}
48. LimitedParticleクラスの実装
• クラス図
• Particleクラスと、それを継承するLimitedParticleクラス
Particle(x:float, y:float, vx:float, vy:float, radius: float, gravity:float)
update(): void
display(): void
x: float
y: float
vx: float
vy: float
radius: float
gravity: float
Particle
LimitedParticle(x:float, y:float, vx:float, vy:float, radius: float,
gravity:float, friction:float)
update(): void
limit(): void
friction: float
LimitedParticle
49. LimitedParticleクラス
class LimitedParticle extends Particle {
float friction = 0.99;
LimitedParticle(int x, int y, float vx, float vy, float radius, float friction) {
super(x, y, vx, vy, radius); //親クラスの引数を参照
this.friction = friction; //摩擦係数
}
void update() {
vy *= friction;
vx *= friction;
super.update(); //
limit();
}
void limit() { //表示範囲内でバウンドする
if (y > height - radius) {
vy = -vy;
y = constrain(y, -height * height, height - radius);
}
if ((x < radius) || (x > width - radius)) {
vx = -vx;
x = constrain(x, radius, width - radius);
}
}
}
50. LimitedParticleクラスを使用する
int num = 80;
LimitedParticle[] p = new LimitedParticle[num];
float radius = 1.2;
void setup() {
size(640, 480);
noStroke();
smooth();
for (int i = 0; i < p.length; i++) {
float velX = random(-4, 4);
float velY = random(-10, 0);
p[i] = new LimitedParticle(width/2, height/2, velX, velY, 2.2, 0.999);
}
}
void draw() {
fill(0, 24);
rect(0, 0, width, height);
fill(255);
for (int i = 0; i < p.length; i++) {
p[i].update();
p[i].display();
}
}
53. 色の付加
• クラス図追加
Particle(x:float, y:float, vx:float, vy:float, radius: float, gravity:float)
update(): void
display(): void
x: float
y: float
vx: float
vy: float
radius: float
gravity: float
Particle
LimitedParticle(x:float, y:float, vx:float, vy:float, radius: float,
gravity:float, friction:float, col:color)
update(): void
limit(): void
display(): void
friction: float
col: color
LimitedParticle
54. 色の付加
class LimitedParticle extends Particle {
float friction = 0.99;
color col; //色の属性
LimitedParticle(int x, int y, float vx, float vy, float radius, float friction, color col) {
super(x, y, vx, vy, radius);
this.friction = friction;
this.col = col;
}
void update() {
vy *= friction;
vx *= friction;
super.update();
limit();
}
void limit() {
if (y > height - radius) {
vy = -vy;
y = constrain(y, -height * height, height - radius);
}
if ((x < radius) || (x > width - radius)) {
vx = -vx;
x = constrain(x, radius, width - radius);
}
}
void display() {
fill(col);
super.display(); //スーパークラス (Particle) の display() を呼び出し
}
}
55. 色の付加
int num = 200;
LimitedParticle[] p = new LimitedParticle[num];
float radius = 1.2;
void setup() {
size(640, 480);
colorMode(HSB,360,100,100,100); //HSBで色を指定する
noStroke();
smooth();
for (int i = 0; i < p.length; i++) {
float velX = random(-4, 4);
float velY = random(-10, 0);
color col = color(random(180,240),random(50,100),50); //ランダムに色指定
p[i] = new LimitedParticle(width / 2, height / 2, velX, velY, 2.2, 0.9999, col);
}
}
void draw() {
fill(0, 10);
rect(0, 0, width, height);
fill(255);
for (int i = 0; i < p.length; i++) {
p[i].update();
p[i].display();
}
}