Java オブジェクト（Object Oriented Programming）とコンストラクタ、スコープ（local,global）

#１，オブジェクトとは
<font color="Red">**Object is an instance of a class that may contain attributes and methods.**</font>
車、人間、サイコロ、ピザのインスタンスを作りながら様々なルールを覚えていきましょう！

#２，ソースコード
><font color="Red">ルール１，mainメソッドとは別のファイルに記述する

mainクラスを記述しているクラスを<font color="Blue">**アプリケーションクラス**</font>、それ以外を、<font color="Blue">**一般クラス**</font>と言う。

```java:Main.java
public class Main {

	public static void main(String[] args) {

		// Object Oriented Programming
		// object = an instance of a class that may contain attributes and methods.

		Car myCar1 = new Car();
		Car myCar2 = new Car();

		System.out.println(myCar1.make);
		System.out.println(myCar1.model);
		System.out.println();
		System.out.println(myCar2.make);
		System.out.println(myCar2.model);

		myCar1.drive();
		myCar1.brake();

	}

}
```
※一般クラスには、mainメソッドは記述しない👇

```java:Car.java
public class Car {

	String make = "Chevrolet";
	String model = "Corvette";
	int year = 2020;
	String color = "blue";
	double price = 50000.00;

	void drive() {
		System.out.println("You drive the car.");
	}

	void brake() {
		System.out.println("You step on the brakes.");
	}

}

```

><font color="Red">ルール２，コンストラクターを作る時は、クラス名と同じメソッド名にし先頭は大文字

```java:Main.java
public class Main {

	public static void main(String[] args) {

		// constructor = special method that is called when an object is instantiated(created).

		Human human1 = new Human("hiro", 25, 70);
		Human human2 = new Human("taro", 20, 65);

		System.out.println(human1.name);
		human1.eat();
		human1.drink();
		System.out.println(human2.name);
		human2.eat();
		human2.drink();

	}

}
```
※public Huan()がコンストラクタになります👇

```java:Human.java
public class Human {

	String name;
	int age;
	double weight;

	public Human(String name, int age, double weight) {
		this.name = name;
		this.age = age;
		this.weight = weight;
	}

	public void eat() {
		System.out.println(this.name + "is eating.");
	}

	public void drink() {
		System.out.println(this.name + "is drinking");
	}

}

```

><font color="Red">ルール３，スコープにも注意

```java:Main.java
public class Main {

	public static void main(String[] args) {

		DiceRoller diceRoller = new DiceRoller();

	}

}
```
※フィールドに書くと、global変数になる👇

```java:DiceRoller.java
import java.util.Random;

public class DiceRoller {

	Random random;
	int number = 0;

	public DiceRoller() {
		random = new Random();
		int number = 0;
		roll();
	}

	void roll() {
		number = random.nextInt(6) + 1;
		System.out.println(number);

	}

}

```

><font color="Red">ルール４，コンストラクタはデータ型、順番、パラメーターの数が重要

```java:Main.java
public class Main {

	public static void main(String[] args) {

		Pizza pizza = new Pizza("thicc crust", "tomato", "mozzerella", "pepperoni");

		System.out.println("Here are the ingredients of your pizza");
		System.out.println(pizza.bread);
		System.out.println(pizza.sauce);
		System.out.println(pizza.cheese);
		System.out.println(pizza.topping);

	}

}

```

※コンストラクタのデータ型、順番、パラメーターの数に注意👇

```java:DiceRoller.java
public class Pizza {

	String bread;
	String sauce;
	String cheese;
	String topping;

	public Pizza() {
	}

	public Pizza(String bread) {
		this.bread = bread;

	}

	public Pizza(String bread, String sauce) {
		this.bread = bread;
		this.sauce = sauce;
	}

	public Pizza(String bread, String sauce, String cheese) {
		this.bread = bread;
		this.sauce = sauce;
		this.cheese = cheese;
	}

	public Pizza(String bread, String sauce, String cheese, String topping) {
		this.bread = bread;
		this.sauce = sauce;
		this.cheese = cheese;
		this.topping = topping;
	}

```

#Java チュートリアル:clipboard:
:coffee: Java テキストファイル（.txt .csv）への出力　java.io.FileWriter : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/2a101f159b1d8447d2cc "here")
:coffee:  Java テキストファイル（.txt .csv）からの入力　java.io.FileReader : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/31c677bb619e73b717a9 "here")
:coffee:  Java モニターからの入力　java.util.Scanner : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/ece84972a49544916cac "here")
:coffee:  Java GUIからの入力　javax.swing.JOptionPane : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/c0b9a2bd9018131b2a48 "here")
:coffee:  Java Mathクラス　Math.sqrt : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/fc5500308a58c3f75887 "here")
:coffee:  Java 乱数の生成　java.util.Random : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/ac357d0fa5b4f70acb9a "here")
:coffee:  Java Stringクラス : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/17b6ea08650d9c8ae072 "here")
:coffee:  Java Wrapperクラス : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/6d2a8480587e40fee90a "here")
:coffee:  Java ArrayListクラス　java.util.ArrayList : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/6478e5851e9a30d0d65c "here")
:coffee:  Java メソッド（method）の作り方とオーバーロード : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/2b132cb5298f9fc97730 "here")
:coffee:  Java printf()メソッド : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/3e471b8006f9500ef3e4 "here")
:coffee:  Java オブジェクト（Object Oriented Programming）とコンストラクタ、スコープ（local,global） : [:white_check_mark:here](https://qiita.com/iwasaki-hub/items/488d41a259aa5a43f921 "here")