Created
December 2, 2016 03:17
-
-
Save kdmgs110/156a42bf8da945792fba7ed3496bc3af to your computer and use it in GitHub Desktop.
オブジェクト指向②Dice.javaとExec.java CHAPTER13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************Dice.java*************************************************/ | |
package pass01; | |
public class Dice { //Diceクラス | |
int val;//フィールド変数 | |
public void play(){ | |
val = (int)(Math.random()*6)+1; //1~6のどれかを代入する | |
} //インスタンスメソッド | |
} | |
/***********************************Exec.java*****************************************/ | |
package pass01; | |
/* | |
* オブジェクト指向P304 | |
* new演算子 | |
* newで作成したオブジェクト=インスタンス、設計図クラスから作成された個々のオブジェクト | |
*/ | |
public class Exec { | |
public static void main(String[] args) { | |
Dice dice = new Dice(); //Dice型 オブジェクトの作成。Dice()をコンストラクタ | |
java dice.val = 1; //クラスメソッドとしてvalが使える。Diceクラスの初期値は0になってる。さいころは1からなので代入 | |
System.out.println("目数"+dice.val); //valをコンソールに表示する。 | |
dice.play(); //diceの目数をラン団無に変更する | |
System.out.println("目数=" + dice.val); | |
} | |
} | |
/*出力 | |
目数1 | |
目数=3 | |
*/ | |
/************コンストラクタとか**************************** | |
http://nobuo-create.net/java-beginner-20/ | |
**********************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment