Example 32 java public static void addint target
引数がいっぱい 先週 (Example 3_2. java) public static void add(int[] target. Array, int add. ID) 例題4 -1 (Example 4_1. java) public static void add(int[] target. Id. Array, String[]target. Name. Array, int[] target. Price. Array, int add. ID, String add. Name, int add. Price)
クラスごとに別ファイルに public class Example 4_1{ } public class Item. Type{ … … } Example 4_1. java … … Item. Type. java ファイル名とクラス名を同じにすること!
オブジェクトを作る n 例題4 -1 オブジェクトはExample 4_1クラスのmain() メソッドで作ります。 public class Example 4_1 { public static void main(String[] args) { //商品種類(コーラ)をインスタンス化する Item. Type cola; cola = new Item. Type(); cola. id = 1001; cola. name = “コーラ”; cola. price=120; } }
Item. Type配列への代入 id=1001 name="コーラ" price=120 [0] [1] [2] [3] [4] [5] 続くよ Item. Type[] item. Type. Array = new Item. Type[10]; //配列を作る item. Type. Array[0] = new Item. Type(); //Item. Typeオブジェクト を作って配列へ代入 item. Type. Array[0]. id = 1001; item. Type. Array[0]. name = “コーラ”; item. Type. Array[0]. price = 120;
入ってない(nullの) 変数に、代入/参照するな! n 次のプログラムを実行してみましょう。 //これはまずいプログラム public class Fail. Program{ public static void main(String[] args){ Item. Type[] item. Type. Array = new Item. Type[10]; //配列を作る //item. Type. Array[0]はnull item. Type. Array[0]. id = 1001; item. Type. Array[0]. name = “コーラ”; item. Type. Array[0]. price = 120; } }
追加メソッドの変更 今回の プログラム 前回までの プログラム public static void add(Item. Type[] target. Array, Item. Type add. Item. Type){ //商品種類が入っていない箱を探して格納する for(int i=0; i<10; i++){ if(target. Array[i] == null){//入っていない target. Array[i] = add. Item. Type; //書き込む break; } } } public static void add(int[] target. Array, int add. Id){ //商品種類が入っていない箱を探して格納する for(int i=0; i<10; i++){ if(target. Array[i] == -1){//入っていない target. Array[i] = add. Id; //書き込む break; } } }
mainメソッドの変更 //商品種類をインスタンス化する //コーラ Item. Type cola = new Item. Type(); cola. id = 1001; cola. name = “コーラ”; cola. price = 120; //ソーダ Item. Type soda = new Item. Type(); soda. id = 1002; soda. name = “ソーダ”; soda. price = 120; //お茶 Item. Type greentea = new Item. Type(); greentea. id = 1003; greentea. name = “お茶”; greentea. price = 120; 例題4 -3 (Example 4_3. java) まだ3つだからいいけど、 たくさんになったら大変だ。 何とか見やすく方法は ないかな?
Javaの文法復習⑧ コンストラクタ n コンストラクタを使って読みやすくする //商品種類をインスタンス化する //コーラ Item. Type cola = new Item. Type(); cola. id = 1001; cola. name = “コーラ”; cola. price = 120; //ソーダ Item. Type soda = new Item. Type(); soda. id = 1002; soda. name = “ソーダ”; soda. price = 120; //お茶 Item. Type greentea = new Item. Type(); greentea. id = 1003; greentea. name = “お茶”; greentea. price = 120;
コンストラクタ(2)引数 例題4 -4 (Item. Type. java) //引数をつけたコンストラクタのあるItem. Typeクラス public class Item. Type{ int id; String name; int price; public Item. Type(int new. Id, String new. Name, int new. Price){ } } コンストラクタにも引数を書くことができます。
コンストラクタ(3)代入 例題4 -4 (Item. Type. java) //引数を代入するコンストラクタを持つItem. Typeクラス public class Item. Type{ int id; String name; int price; public Item. Type(int new. Id, String new. Name, int new. Price){ id = new. Id; name = new. Name; price = new. Price; } } id, name, priceに値が代入されます。
属性はクラススコープの変数 //引数を代入するコンストラクタを持つItem. Typeクラス public class Item. Type{ int id; String name; int price; public Item. Type(int new. Id, String new. Name, int new. Price){ id = new. Id; name = new. Name; price = new. Price; } } idとnameとpriceの有効範囲
導入したItem. Type. Listクラス 例題4 -5 (Example 4_5. java) public class Item. Type. List { //商品種類を追加するメソッド public void add(Item. Type[] target. Array, Item. Type add. Item. Type){ //商品種類が入っていない箱を探す for(int i=0; i<10; i++){ if(target. Array[i] == null){//入っていない target. Array[i] = add. Item. Type; //書き込む break; } } } インスタンス化する オブジェクトの メソッドには staticがいりません //商品種類を表示するメソッド public void display(Item. Type[] target. Array){ for(int i=0; i<10; i++){ if(target. Array[i] != null){//商品が入っている System. out. println(target. Array[i]. name+"は販売中です"); } }
メソッドが main()だけになったExample 例題4 -5 (Example 4_5. java) public class Example 4_5 { public static void main(String[] args) { //自動販売機プログラムの開始を知らせる System. out. println("自動販売機が開始しました。"); //商品種類を保存するための配列を定義する Item. Type[] item. Type. Array = new Item. Type[10]; //商品種類リストを生成する Item. Type. List item. Type. List = new Item. Type. List(); インスタンス化された オブジェクトの メソッドの呼び出し //商品種類を追加する item. Type. List. add(item. Type. Array, new Item. Type(1001, "コーラ")); item. Type. List. add(item. Type. Array, new Item. Type(1022, "ソーダ")); item. Type. List. add(item. Type. Array, new Item. Type(1033, "お茶")); オブジェクト名. メソッド名() //商品種類リストを表示する item. Type. List. display(item. Type. Array); } }
- Slides: 47