LoginSignup
0
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-11-28

1,オブジェクトとは

Object is an instance of a class that may contain attributes and methods.
車、人間、サイコロ、ピザのインスタンスを作りながら様々なルールを覚えていきましょう!

2,ソースコード

ルール1,mainメソッドとは別のファイルに記述する

mainクラスを記述しているクラスをアプリケーションクラス、それ以外を、一般クラスと言う。

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メソッドは記述しない👇

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.");
    }

}

ルール2,コンストラクターを作る時は、クラス名と同じメソッド名にし先頭は大文字

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()がコンストラクタになります👇

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");
    }

}

ルール3,スコープにも注意

Main.java
public class Main {

    public static void main(String[] args) {

        DiceRoller diceRoller = new DiceRoller();

    }

}

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

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);

    }

}

ルール4,コンストラクタはデータ型、順番、パラメーターの数が重要

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);

    }

}

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

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
:coffee: Java テキストファイル(.txt .csv)からの入力 java.io.FileReader : :white_check_mark:here
:coffee: Java モニターからの入力 java.util.Scanner : :white_check_mark:here
:coffee: Java GUIからの入力 javax.swing.JOptionPane : :white_check_mark:here
:coffee: Java Mathクラス Math.sqrt : :white_check_mark:here
:coffee: Java 乱数の生成 java.util.Random : :white_check_mark:here
:coffee: Java Stringクラス : :white_check_mark:here
:coffee: Java Wrapperクラス : :white_check_mark:here
:coffee: Java ArrayListクラス java.util.ArrayList : :white_check_mark:here
:coffee: Java メソッド(method)の作り方とオーバーロード : :white_check_mark:here
:coffee: Java printf()メソッド : :white_check_mark:here
:coffee: Java オブジェクト(Object Oriented Programming)とコンストラクタ、スコープ(local,global) : :white_check_mark:here

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0