Backend/JAVA2 멘토시리즈

JAVA Eclipse 23 클래스 생성자

쏠솔랄라 2023. 5. 9. 09:16

 

 

생성자

 

: 객체 생성 시 호출되어, 변수들을 초기화화는 메서드

 

클래스와 이름이 같음

리턴 타입, 반환값이 없음

 

구현부
클래스명 ( ) { }

호출부
클래스명 ( );

 

ex1.

구현부

Aclass (){

   x=100;

}

 

호출부

new Aclass():

 

 

ex2.

public class Constructor {

public static void main(String[] args) {

Cellphone phone = new Cellphone();

}

}

 

class Cellphone {

String model="Galaxy S23 Ultra";

String color="Baby Pink";

int capacity=512;

 

Cellphone () {

System.out.println("model: " + model);

System.out.println("color: " + color);

System.out.println("capacity: " + capacity);

}

}

 

 

출력화면

 

 

매개변수 생성자

 

구현부
클래스명(자료형 변수명) { }

호출부
클래스명(값);

 

 

ex.

public class Constructors {

public static void main(String[] args) {

 

Bclass b1 = new Bclass("김땡땡");

System.out.println(b1.name);

}

}

class Bclass {

 

String name;

 

Bclass (String name){ // 매개변수 생성자

System.out.println("Bclass의 매개변수 생성자()");

this.name=name;

 

// this : 현재 객체를 지칭하기 위한 키워드

// 매개변수의 변수명과 객체 내 변수의 이름이 같을 경우 this를 사용해서 구분

}

}

 

 

출력화면

 

 


 

 

Exercise 1

 

public class PhoneConstructor {

public static void main(String[] args) {

 

Iphone myphone1 = new Iphone();

Iphone myphone2 = new Iphone("iPhone", "white", 256);

 

System.out.println(myphone1.model + myphone1.color + myphone1.capacity);

System.out.println(myphone2.model);

 

myphone2.info();

 

}

}

 

class Iphone {

String model;

String color;

int capacity;

 

Iphone (){

 

}

 

Iphone (String model, String color, int capacity){

this.model=model;

this.color=color;

this.capacity=capacity;

}

 

void info() {

System.out.println("model: " + model);

System.out.println("color: " + color);

System.out.println("capacity: " + capacity);

}

}

 

 

출력화면

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

JAVA Eclipse 09 제어문 : 반복문 for
https://developernew.tistory.com/102

JAVA Eclipse 10 제어문 : 반복문 while, do-while
https://developernew.tistory.com/103

JAVA Eclipse 11 제어문 : 반복문의 break, continue
https://developernew.tistory.com/106

JAVA Eclipse 12 배열 : 배열의 개념 및 사용
https://developernew.tistory.com/107

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기
https://developernew.tistory.com/115

JAVA Eclipse 14 배열 : 다차원배열
https://developernew.tistory.com/117

JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120

JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기
https://developernew.tistory.com/124

JAVA Eclipse 17 method 메서드(메소드)
https://developernew.tistory.com/126

JAVA Eclipse 18 메서드(메소드) 오버로딩
https://developernew.tistory.com/133

JAVA Eclipse 19 객체지향 언어
https://developernew.tistory.com/135

JAVA Eclipse 20 클래스와 객체
https://developernew.tistory.com/155

JAVA Eclipse 21 인스턴스 변수와 클래스 변수
https://developernew.tistory.com/156

22 객체 타입 배열
https://developernew.tistory.com/157