생성자와 메소드 예제

필드 선언 및 기본 생성자

■ 자동자바

package d_constructor;

public class Car {
	
	String company;		// 제조사
	String model;		// 모델명
	String color;		// 색상
	int maxSpeed;		// 최대 속도
	int speed;			// 현재 속도
	
	Car(){
		System.out.println("Car 기본 생성자 호출");
	}

방법

매개변수가 3개인 생성자 호출

	Car(String company, String model, String color){
		this.company = company;
		this.model = model;
		this.color= color;
		System.out.println("3개의 매개변수를 넘겨받는 생성자 호출");
	}

alt + s + 생성자 자동 완성

생성자의 자동 완성 시

메서드는 선언된 필드로 자동 생성됩니다.

빌드할 때 전체 또는 일부만 실행하도록 선택할 수 있습니다.

public Car(String company, 
				String model, String color, 
				int maxSpeed, int speed) {
		this(company,model,color);
		this.maxSpeed = maxSpeed;
		this.speed = speed;
	}

■ AutoExample.java

Car.java car라는 변수에 할당된 Car 클래스

동일한 패키지 내에서 작업

package d_constructor;

public class CarExample {

	public static void main(String() args) {
		Car car = new Car();

3개만 지정된 메서드 호출

Car k5 = new Car("KIA","K5",280);

alt +s +자동 생성 호출

Car k3 = new Car("Hyundai", "AVANTE", "WHITE", 240, 0);