1 / 24

アルゴリズムとプログラミング (Algorithms and Programming)

アルゴリズムとプログラミング (Algorithms and Programming). クラスの宣言 アクセス修飾子 インスタンスの生成 (new キーワード) this キーワード フィールドとメソッドの実際の定義と使い方. 第6回:クラスとインスタンス. 講義資料等:. http://www.pe.titech.ac.jp/~watanabe/lecture/ap/index-j.html. クラスの宣言. Java におけるクラスの宣言. UML におけるクラス図. class クラス名 { }. クラス名. 猫. フィールド の宣言

marcus
Download Presentation

アルゴリズムとプログラミング (Algorithms and Programming)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. アルゴリズムとプログラミング(Algorithms and Programming) • クラスの宣言 • アクセス修飾子 • インスタンスの生成(newキーワード) • thisキーワード • フィールドとメソッドの実際の定義と使い方 第6回:クラスとインスタンス 講義資料等: http://www.pe.titech.ac.jp/~watanabe/lecture/ap/index-j.html

  2. クラスの宣言 Javaにおけるクラスの宣言 UMLにおけるクラス図 class クラス名{ } クラス名 猫 フィールドの宣言 .....; .....; 尾の長さ 体重 ひげの本数 属性 (attribute) 鳴く() 食べる() 歩く() 操作 (operation) メソッドの宣言 .....; .....;

  3. 猫(Cat)クラスの宣言 • クラス名を決める • フィールド名とその型、可視性を決める • メソッドを定義する(やり方は後述) 猫(Cat) 尾の長さ(length):double 体重(weight):double ひげの本数(whisker):int クラス名、フィールド名は識別子なので 命名規則は変数と同じ

  4. 識別子の命名規則 変数名,クラス名など • 使える文字は半角文字の英字・数字・アンダースコア「_」に限定(例外は多いが避けた方が無難) • 半角英字の大文字・小文字は区別される • 数字で始まる識別子は認めない • キーワード(keyword)と呼ばれるJavaの予約語と、true・false・nullの3単語は使用できない。(他の語句と組み合わせれば可) • 長さに制限はない

  5. Javaにおける可視性 UMLでは3種類だがJavaでは4種類 猫(Cat) +尾の長さ(length):double #体重(weight):double -ひげの本数(whisker):int

  6. Catクラスの宣言 クラス図 Javaにおけるクラスの宣言文 猫(Cat) class Cat { public double length; protected double weight; private int whisker; } +尾の長さ(length):double #体重(weight):double -ひげの本数(whisker):int (とりあえずフィールドのみの例)

  7. Boxクラスの宣言 クラス図 クラス宣言文 class Box { } Box -id_number: long -width:double #depth:double +height:double

  8. Boxクラスの宣言 クラス図 クラス宣言文 class Box { private long id_number; private double width; protected double depth; public double height; } Box -id_number: long -width:double #depth:double +height:double

  9. インスタンスの生成 まず、インスタンスを格納する変数を定義する. 変数を定義するには、変数名と型が必要 変数名:識別子の命名規則に従って決める 型:クラス名がそのまま型になる Catクラスのインスタンスを格納するための変数 p を定義 Cat p; (まだ実際のインスタンスは存在しない) オブジェクト参照型変数

  10. インスタンスの生成(II) p newキーワード これで新たにインスタンスが 生成された! p = new Cat(); メモリ領域を確保 0 1 2 番地 Java VMが内部でやっていること データ • まず、Catクラスのインスタンスを格納するために必要なメモリ領域を新たに確保する(new) 。 • 確保したメモリ領域に、初期値を代入する • そのメモリ先頭番地を変数pに格納する • 変数pにはインスタンスの中身は格納されておらず、その先頭番地のみが格納されている • プログラマは、変数pを通してインスタンスの中身を参照したり変更したりする p 変数の中に先頭番地が格納される 標識が立っているようなもの 0 1 2 番地 データ

  11. newキーワードの使い方 Cat p; p = new Cat(); 変数の宣言と代入は1行でも書ける Cat p = new Cat();

  12. フィールドの値を参照する書式 オブジェクト参照型変数.フィールド名 ピリオド 書式例) p.length 実際に参照できるかどうかは アクセス制限に依存する

  13. フィールドへのアクセス(例) weightフィールドに 値2000.0を代入する class Cat { public double length; protected double weight; private int whisker; } p.weight=2000.0; ピリオドの後にフィールド名を指定する lengthフィールドの値を表示 System.out.println( p.length );

  14. メソッドの定義と利用 Javaにおけるクラスの宣言 クラス図 class クラス名{ } クラス名 猫 フィールドの宣言 .....; .....; 尾の長さ 体重 ひげの本数 属性 (attribute) 鳴く() 食べる() 歩く() 操作 (operation) メソッドの宣言 .....; .....;

  15. メソッドの定義 • メソッドは引数と、戻り値を持つ • 型は厳しくチェックされる • 複数の戻り値は持てない 書式: 戻り値の型 メソッド名(型 引数, 型 引数,...) {   メソッドの処理の記述 return 戻り値; } C言語の「関数」と類似だが、型のチェックはずっと厳しい

  16. 2つの引数を受け取ってその和を戻り値として返すメソッド2つの引数を受け取ってその和を戻り値として返すメソッド int addValue ( int a, int b ) { return a+b ; } 仮引数 戻り値の型 戻り値 必ずしも( )で囲う必要はない

  17. 戻り値が不要の場合 • 戻り値の型をvoidと記述する • return文は省略可 void printValue ( int a, int b ) { System.out.println( "a=" + a ); System.out.println( "b=" + b ); }

  18. 引数も不要の場合 • 引数の記述は省略可 void printHello () { System.out.println( "Hello!" ); }

  19. 例) class Cat { public double weight; private int whisker; void printField() { System.out.println("体重は"+weight+"g"); System.out.println("ひげは"+whisker+"本"); } void setValue(double w, int n ) { weight = w; whisker = n ; } int getWeightKg(){ return (weight * 0.001 ) ; } } フィールド メソッド

  20. thisキーワード • あるクラス宣言の中で使用され、そのクラスから生成されたオブジェクト自身を指す this.weight などが厳密な書き方 だが普通は省略される this.whisker

  21. 例) class Cat { public double weight; private int whisker; void printField() { System.out.println("体重は"+weight+"g"); System.out.println("ひげは"+whisker+"本"); } void setValue(double w, int n ) { weight = w; whisker = n ; } int getWeightKg(){ return (weight * 0.001 ) ; } } 同一クラス内なら privateでも参照可 厳密にはthis.weight だが通常はthisを省略

  22. メソッドの呼び出し方 先のCatクラス宣言文の後に追加して SampleCat.javaとする class SampleCat { public static void main(String[] args) { Cat p = new Cat(); p.setValue( 1000.0, 8 ); p.printField(); int w_kg = p.getWeightKg(); System.out.println("体重は"+ w_kg +"kg"); } } 引数がある場合 戻り値がある場合

  23. メソッドの呼び出し方 オブジェクト参照型変数.メソッド名(引数,引数,...) ピリオド 実際に呼び出せるかどうかは アクセス制限に依存する

  24. まとめ • クラスの宣言 • アクセス修飾子 • フィールドの定義 • メソッドの定義 • インスタンスの生成(newキーワード) • thisキーワード • フィールドとメソッドの使用例

More Related