Presentation is loading. Please wait.

Presentation is loading. Please wait.

第2章 Eclipseと簡単なオブジェクト 指向プログラミング

Similar presentations


Presentation on theme: "第2章 Eclipseと簡単なオブジェクト 指向プログラミング"— Presentation transcript:

1 第2章 Eclipseと簡単なオブジェクト 指向プログラミング
オブジェクト指向Javaプログラミング入門 第2章  Eclipseと簡単なオブジェクト 指向プログラミング 近代科学社©2008 Toru Kato Masahiro Higuchi Shiro Takata

2 今日の内容 1. 実施要領の把握(実習ノートを見ながら説明する) 2. Eclipse使用方法の習得 3. 簡単なオブジェクト指向プログラミング 4. 演習課題 の作成と提出

3 1. 実施要領の把握(実習ノートを見ながら説明する) 2. Eclipse使用方法の習得 (教科書を見ながら実際に使用する)
今日の内容 1. 実施要領の把握(実習ノートを見ながら説明する) 2. Eclipse使用方法の習得   (教科書を見ながら実際に使用する) p.10 Eclipse の起動  p.13 ソースコードはEUCのまま   p.14 プロジェクトの作成 p.15 HelloWorldクラスの作成

4 1. 実施要領の把握(実習ノートを見ながら説明する) 2. Eclipse使用方法の習得 3. 簡単なオブジェクト指向プログラミング
今日の内容 1. 実施要領の把握(実習ノートを見ながら説明する) 2. Eclipse使用方法の習得 3. 簡単なオブジェクト指向プログラミング (教科書を見ながら実際に使用する) p.18 クラスとは  p.19 Rectangleクラス

5 オブジェクトをクラス(class)に分類
オブジェクト指向 世の中にあるものは,全てオブジェクト オブジェクトをクラス(class)に分類 class 人間 機器 乗り物 建物 動物 植物 オブジェクトを主体としてプログラムを記述

6 オブジェクト指向プログラミング クラスRectangle x座標 y座標 幅 属性 高さ できること 移動する 状態を表示
オブジェクト指向プログラミングでは、まず対象とするもの(例えば座標平面上の長方形)の特徴を抽出し、クラスを作ることから始める。 x座標 y座標 高さ できること  移動する 状態を表示 属性

7 オブジェクト指向プログラミング オブジェクト クラスRectangle rectangle1 x座標 2 y座標 1 幅 5 属性 高さ 6
できること  移動する 状態を表示 rectangle1 属性 2 1 5 6 具体的な「rectangle1」は、Rectangleクラスのオブジェクトである。各属性に具体的な値を持つ。

8 オブジェクト指向プログラミング オブジェクト クラスRectangle rectangle1 x座標 2 y座標 1 幅 5 属性 高さ 6
できること  移動する 状態を表示 rectangle1 属性 2 1 5 6 そして、そのオブジェクトに 仕事をさせる。

9 オブジェクト指向プログラミング オブジェクト クラスRectangle x座標 y座標 高さ できること  移動する 状態を表示 rectangle1 属性 2 1 5 6 状態を表示して!

10 オブジェクト指向プログラミング オブジェクト クラスRectangle rectangle1 x座標 2 y座標 1 幅 5 属性 高さ 6
できること  移動する 状態を表示 rectangle1 属性 2 1 5 6 x座標2, y座標1, 幅5, 高さ6

11 オブジェクト指向プログラミング オブジェクト クラスRectangle rectangle1 x座標 2 y座標 1 幅 5 属性 高さ 6
できること  移動する 状態を表示 rectangle1 属性 2 1 5 6 x座標方向に2 y座標方向に2 だけ移動して!

12 オブジェクト指向プログラミング オブジェクト クラスRectangle rectangle1 x座標 y座標 高さ できること  移動する 状態を表示 属性 =4 =3 5 6

13 オブジェクト指向プログラミング オブジェクト クラスRectangle x座標 y座標 幅 属性 高さ できること 移動する 状態を表示
できること  移動する 状態を表示 属性 一つのクラスの、幾つものオブジェクトを作ることができる。

14 オブジェクト指向プログラミング オブジェクト クラスRectangle x座標 y座標 幅 属性 高さ できること 移動する 状態を表示
できること  移動する 状態を表示 属性 オブジェクトたちに仕事をさせることで問題を解決する

15 簡単なオブジェクト指向プログラム p. 20 プログラム例2.2.1 1 public class Rectangle {
private int width; private int height; private int xPosition; private int yPosition; 6 public Rectangle(int width, int height, int xPosition, int yPosition) { this.width = width; this.height = height; this.xPosition = xPosition; this.yPosition = yPosition; } 13 public void move(int xMove, int yMove) { xPosition = xPosition + xMove; yPosition = yPosition + yMove; } 18 public void printInfo() { System.out.print(" x座標: " + xPosition + ", y座標 : " + yPosition); System.out.println(", 幅: " + width + ", 高さ: " + height); } 23 public static void main(String[] args) { Rectangle rectangle1 = new Rectangle(2, 1, 5, 6); 26 rectangle1.printInfo(); rectangle1.move(2, 2); rectangle1.printInfo(); } 31 } p. 20 プログラム例2.2.1

16 簡単なオブジェクト指向プログラム 作成方法 教科書 p. 20 2行目 実行方法 教科書 p. 20 下から3行目 実行結果
説明の前に、まず作成して実行してみよう 作成方法  教科書 p. 20 2行目 実行方法  教科書 p. 20 下から3行目 実行結果 x座標2, y座標1, 幅5, 高さ6 x座標4, y座標3, 幅5, 高さ6

17 フィールド 1 public class Rectangle { 2 private int xPosition;
private int yPosition; private int width; private int height; public void move(int xMove, int yMove) { xPosition = xPosition + xMove; yPosition = yPosition + yMove; } public void printInfo() { System.out.print(" x座標: "+xPosition + ", y座標 : " yPosition); System.out.println(", 幅: " + width + ", 高さ: " + height); } フィールド

18 メソッド 1 public class Rectangle { 2 private int xPosition;
private int yPosition; private int width; private int height; public void move(int xMove, int yMove) { xPosition = xPosition + xMove; yPosition = yPosition + yMove; } public void printInfo() { System.out.print(" x座標: "+xPosition + ", y座標 : " yPosition); System.out.println(", 幅: " + width + ", 高さ: " + height); } メソッド

19 オブジェクトの生成方法を、コンストラクタによって定義する。
クラスRectangle x座標 y座標 高さ できること  移動する 状態を表示 属性 2 1 5 6

20 コンストラクタ 1 public class Rectangle { 2 private int xPosition;
private int yPosition; private int width; private int height; public Rectangle(int xPosition, int yPosition, int width, int height) { this.xPosition = xPosition; this.yPosition = yPosition; this.width = width; this.height = height; } コンストラクタ

21 mainメソッドで実行 24 public static void main(String[] args) {
Rectangle rectangle1 =                 new Rectangle(2, 1, 5, 6); 26 rectangle1.printInfo(); rectangle1.move(2, 2); rectangle1.printInfo(); } 31 }

22 オブジェクトの生成 オブジェクト クラスRectangle x座標 2 y座標 1 幅 5 属性 高さ 6
public static void main(String[] args) { Rectangle rectangle1 =                 new Rectangle(2, 1, 5, 6); オブジェクト クラスRectangle x座標 y座標 高さ 属性 2 1 5 6

23 コンストラクタ new Rectangle(2, 1, 5, 6); 2 1 5 6 1 public class Rectangle {
private int xPosition; private int yPosition; private int width; private int height; public Rectangle(int xPosition, int yPosition, int width, int height) { this.xPosition = xPosition; this.yPosition = yPosition; this.width = width; this.height = height; } 5 6 コンストラクタ

24 オブジェクトの生成 オブジェクト クラスRectangle x座標 2 y座標 1 幅 5 属性 高さ 6
public static void main(String[] args) { Rectangle rectangle1 =                  new Rectangle(2, 1, 5, 6); rectangle1.printInfo(); rectangle1.move(2, 2); rectangle1.printInfo(); } 26 } オブジェクト クラスRectangle x座標 y座標 高さ 属性 2 1 5 6

25 オブジェクトにメソッドを実行させる 実行結果 x座標: 2, y座標: 1 ,幅: 5, 高さ: 6
public static void main(String[] args) { Rectangle rectangle1 =                 new Rectangle(2, 1, 5, 6); rectangle1. printInfo() ; rectangle1. move(2, 2) ; rectangle1. printInfo() ; } 26 } x座標: 2, y座標: 1 ,幅: 5, 高さ: 6 x座標: 4, y座標: 3 ,幅: 5, 高さ: 6 実行結果

26 オブジェクトを複数生成するように拡張した
今日の内容 1. 実施要領の把握(実習ノートに詳述) 2. Eclipse使用方法の習得 3. 簡単なオブジェクト指向プログラミング 4. 演習課題 の作成と提出 教科書(p. 34)の演習問題 2.1 オブジェクトを複数生成するように拡張した Rectangle.javaを作成。実行確認ができたら提出。


Download ppt "第2章 Eclipseと簡単なオブジェクト 指向プログラミング"

Similar presentations


Ads by Google