- クラスはフィールドとメソッドをメンバとしてもちます
- クラス宣言をもとにしてオブジェクトを作成することができます
- オブジェクトを作成してフィールドとすることができます
- クラス内に一定の処理をまとめて、メソッドとして定義することができます
- メソッドに引数をわたして処理させることができます
- メソッドの呼び出し元は、戻り値を受け取ることができます
Last active
December 27, 2015 18:09
-
-
Save shigemk2/7367292 to your computer and use it in GitHub Desktop.
やさしいJava 8
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; | |
} | |
class Sample1 | |
{ | |
public static void main(String[] args) | |
{ | |
// オブジェクト作成 | |
Car car1; | |
car1 = new Car(); | |
// ナンバーとガソリン量を代入 | |
car1.num = 1234; | |
car1.gas = 20.5; | |
System.out.println("車のナンバーは"+car1.num+"です"); | |
System.out.println("ガソリン量は"+car1.gas+"です"); | |
} | |
} |
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 Sample2 | |
{ | |
public static void main(String[] args) | |
{ | |
// オブジェクト作成 | |
Car car1; | |
car1 = new Car(); | |
// ナンバーとガソリン量を代入 | |
car1.num = 1234; | |
car1.gas = 20.5; | |
car1.show(); | |
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 | |
{ | |
// Carクラスの宣言(仕様)です | |
int num; | |
double gas; | |
// メソッド呼び出す部分の処理を行うと、 | |
// メソッド内部にうつって処理が行われる | |
// メソッド内の処理が終わると、呼び出し元に戻って続きを処理する | |
void show() | |
{ | |
// thisはあってもなくてもいい。なぜなら自分自身だから | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
void showCar() | |
{ | |
System.out.println("これから車の情報を表示します"); | |
show(); | |
} | |
} | |
class Sample3 | |
{ | |
public static void main(String[] args) | |
{ | |
// オブジェクト作成 | |
Car car1; | |
car1 = new Car(); | |
// ナンバーとガソリン量を代入 | |
car1.num = 1234; | |
car1.gas = 20.5; | |
car1.showCar(); | |
} | |
} |
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 setNum(int n) | |
{ | |
num = n; | |
System.out.println("ナンバーを"+num+"にしました"); | |
} | |
// 引数(仮引数)を持つメソッド | |
void setGas(double g) | |
{ | |
gas = g; | |
System.out.println("ガソリン量を"+gas+"にしました"); | |
} | |
// 引数をもたないメソッド | |
void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample4 | |
{ | |
public static void main(String[] args) | |
{ | |
// オブジェクト作成 | |
Car car1; | |
car1 = new Car(); | |
// ナンバーとガソリン量を代入(実引数) | |
car1.setNum(1234); | |
car1.setGas(20.5); | |
} | |
} |
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 setNumGas(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"にガソリン量を"+gas+"にしました"); | |
} | |
// 引数をもたないメソッド | |
void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample5 | |
{ | |
public static void main(String[] args) | |
{ | |
// オブジェクト作成 | |
Car car1; | |
car1 = new Car(); | |
int number = 1234; | |
double gasoline = 20.5; | |
// ナンバーとガソリン量を代入(実引数) | |
car1.setNumGas(number, gasoline); | |
// 指定された数以外の数の引数は渡せない | |
// car1.setNumGas(number); | |
} | |
} |
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; | |
// int型の値を返すメソッド | |
int getNum() | |
{ | |
System.out.println("ナンバーを調べました"); | |
return num; | |
} | |
double getGas() | |
{ | |
System.out.println("ガソリン量を調べました"); | |
return gas; | |
} | |
void setNumGas(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("ナンバーを"+num+"にガソリン量を"+gas+"にしました"); | |
} | |
// 引数をもたないメソッド | |
void show() | |
{ | |
System.out.println("車のナンバーは"+this.num+"です"); | |
System.out.println("ガソリン量は"+this.gas+"です"); | |
} | |
} | |
class Sample6 | |
{ | |
public static void main(String[] args) | |
{ | |
// オブジェクト作成 | |
Car car1; | |
car1 = new Car(); | |
// ナンバーとガソリン量を代入(実引数) | |
car1.setNumGas(1234, 20.5); | |
int number = car1.getNum(); | |
double gasoline = car1.getGas(); | |
System.out.println("サンプルから車を調べたところ"); | |
System.out.println("ナンバーは"+number+"ガソリン量は"+gasoline+"でした"); | |
} | |
} |
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 | |
{ | |
int num; | |
double gas; | |
void setNumGas(int n, double g) | |
{ | |
num = n; | |
gas = g; | |
System.out.println("車のナンバーを"+num+"にガソリン量を"+gas+"にしました"); | |
} | |
void show() | |
{ | |
System.out.println("車のナンバーは"+num+"です"); | |
System.out.println("ガソリン量は"+gas+"です"); | |
} | |
} | |
class SampleP1 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car(); | |
car1.setNumGas(1234, 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 MyPoint | |
{ | |
int x; | |
int y; | |
void setX(int px) | |
{ | |
x = px; | |
} | |
void setY(int py) | |
{ | |
y = py; | |
} | |
int getX() | |
{ | |
return x; | |
} | |
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