Last active
December 27, 2015 18:19
-
-
Save shigemk2/7369464 to your computer and use it in GitHub Desktop.
やさしいJava 9
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
// 車クラス | |
// 状態 性質をフィールドとし、機能はメソッドとする | |
// これらをまとめてクラスのメンバという。 | |
class Car | |
{ | |
// Carクラスの宣言(仕様)です | |
int num; | |
double gas; | |
// メソッド呼び出す部分の処理を行うと、 | |
// メソッド内部にうつって処理が行われる | |
// メソッド内の処理が終わると、呼び出し元に戻って続きを処理する | |
void show() | |
{ | |
// thisはあってもなくてもいい。なぜなら自分自身だから | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample1 | |
{ | |
public static void main(String[] args) | |
{ | |
// オブジェクト作成 | |
Car car1; | |
car1 = new Car(); | |
// ナンバーとガソリン量を代入 | |
car1.num = 1234; | |
car1.gas = -20.5; | |
car1.show(); | |
} | |
} |
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
// カプセル化 | |
// 車クラス | |
class Car | |
{ | |
private int num; | |
private double gas; | |
public void setNumGas(int n, double g) | |
{ | |
if (g > 0 && g < 1000) { | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"にガソリン量を"+gas+"にしました"); | |
} | |
else { | |
System.out.println(g+"は正しいガソリン量ではありませんでした"); | |
System.out.println("ガソリン量を変更できませんでした"); | |
} | |
} | |
void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample2 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1; | |
car1 = new Car(); | |
// アクセスできなくなる | |
// car1.num = 1234; | |
// car1.gas = -20.5; | |
car1.show(); | |
car1.setNumGas(1234, 20.5); | |
car1.show(); | |
System.out.println("正しくないガソリン量-10.0を指定してみます"); | |
car1.setNumGas(1234,-10.0); | |
car1.show(); | |
} | |
} |
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
// メソッドのオーバーロード | |
// ポリモーフィズム | |
// 車クラス | |
class Car | |
{ | |
private int num; | |
private double gas; | |
public void setCar(int n) | |
{ | |
num = n; | |
System.out.println("ナンバーを"+num+"にしました"); | |
} | |
public void setCar(double g) | |
{ | |
gas = g; | |
System.out.println("ガソリン量を"+gas+"にしました"); | |
} | |
public void setCar(int n, double g) | |
{ | |
num = n; | |
if (g > 0 && g < 1000) { | |
gas = g; | |
System.out.println("ナンバーを"+num+"にガソリン量を"+gas+"にしました"); | |
} | |
else { | |
System.out.println(g+"は正しいガソリン量ではありませんでした"); | |
System.out.println("ガソリン量を変更できませんでした"); | |
} | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample3 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1; | |
car1 = new Car(); | |
car1.show(); | |
car1.setCar(1234, 20.5); | |
car1.show(); | |
System.out.println("車のナンバーだけ変更"); | |
car1.setCar(2345); | |
car1.show(); | |
System.out.println("ガソリン量だけ変更"); | |
car1.setCar(30.5); | |
car1.show(); | |
} | |
} |
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
// コンストラクタ | |
// 車クラス | |
class Car | |
{ | |
private int num; | |
private double gas; | |
// オブジェクトを作成するとコンストラクタが呼び出されます | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
System.out.println("車を作成しました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample4 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car(); | |
car1.show(); | |
} | |
} |
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
// コンストラクタのオーバーロード | |
// 車クラス | |
class Car | |
{ | |
private int num; | |
private double gas; | |
// オブジェクトを作成するとコンストラクタが呼び出されます | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
System.out.println("車を作成しました"); | |
} | |
public Car(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバー"+num+"ガソリン量"+gas+"の車を作成しました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample5 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car(); | |
car1.show(); | |
Car car2 = new Car(1234, 20.5); | |
car2.show(); | |
} | |
} |
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
// 別のコンストラクタを呼び出す | |
// 車クラス | |
class Car | |
{ | |
private int num; | |
private double gas; | |
// オブジェクトを作成するとコンストラクタが呼び出されます | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
System.out.println("車を作成しました"); | |
} | |
public Car(int n, double g) | |
{ | |
// 引数のないコンストラクタを呼び出す | |
// このおまじないは必ずコンストラクタ内の先頭に配置しておく必要がある | |
this(); | |
num = n; | |
gas = g; | |
System.out.println("ナンバー"+num+"ガソリン量"+gas+"の車を作成しました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample6 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car(); | |
car1.show(); | |
Car car2 = new Car(1234, 20.5); | |
car2.show(); | |
} | |
} |
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
// インスタンス変数インスタンスメソッド | |
// 車クラス | |
class Car | |
{ | |
private int num; | |
private double gas; | |
// オブジェクトを作成するとコンストラクタが呼び出されます | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
System.out.println("車を作成しました"); | |
} | |
public void setCar(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample7 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car(); | |
car1.setCar(1234,20.5); | |
car1.show(); | |
Car car2 = new Car(); | |
car1.setCar(4567,30.5); | |
car2.show(); | |
} | |
} |
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
// クラス変数 クラスメソッド | |
// クラスメソッドはそのクラスからオブジェクトが作成されていなくてもメソッドを呼び出すことが出来る | |
// しかしクラスメソッドでthisは使えない | |
// さらにインスタンス変数にアクセスできない。 | |
// 車クラス | |
class Car | |
{ | |
// クラス変数 | |
public static int sum = 0; | |
private int num; | |
private double gas; | |
public Car() | |
{ | |
num = 0; | |
gas = 0.0; | |
sum++; // コンストラクタが呼び出されたときにクラス変数sumの値を1増やす | |
System.out.println("車を作成しました"); | |
} | |
public void setCar(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました"); | |
} | |
public static void showSum() // クラスメソッド | |
{ | |
System.out.println("車は全部で"+sum+"台あります"); | |
} | |
public void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample8 | |
{ | |
public static void main(String[] args) | |
{ | |
Car.showSum(); // クラスメソッドを呼び出す | |
Car car1 = new Car(); | |
car1.setCar(1234,20.5); | |
car1.show(); | |
Car.showSum(); // クラスメソッドを呼び出す | |
Car car2 = new Car(); | |
car1.setCar(4567,30.5); | |
car2.show(); | |
Car.showSum(); // クラスメソッドを呼び出す | |
} | |
} |
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
// 座標クラス | |
class MyPoint | |
{ | |
private int x; | |
private int y; | |
public MyPoint() | |
{ | |
x = 0; | |
y = 0; | |
} | |
public MyPoint(int px, int py) | |
{ | |
x = px; | |
y = py; | |
} | |
public void setX(int px) | |
{ | |
x = px; | |
} | |
public void setY(int py) | |
{ | |
y = py; | |
} | |
public int getX() | |
{ | |
return x; | |
} | |
public int getY() | |
{ | |
return y; | |
} | |
} | |
class SampleP5 | |
{ | |
public static void main(String[] args) | |
{ | |
MyPoint mypoint = new MyPoint(); | |
mypoint.setX(10); | |
mypoint.setY(5); | |
System.out.println("X座標は"+mypoint.getX()+"です"); | |
System.out.println("Y座標は"+mypoint.getY()+"です"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment