각 객체는 속성(Properties)과 행동(Methods)을 가지며 이들은 각각 객체의 상태와 객체가 수행할 수 있는 작업을 나타낸다.
객체를 프로그래밍 세상에 맞게 모델링을 하기 위해서는 추상화 과정을 진행해야 한다.
클래스란 객체를 생성하기 위한 설계도 역할
클래스는 변수와 함수(메서드)로 구성된다.
변수는 객체의 상태를
메서더는 객채의 행동을 정의한다.
객체를 만들기 위해
먼저 클래스부터 만들어야 한다.
코드를 설계하는 측 / 코드를 실행시키는 측
. <- 객체로 들어가는 연산자
일반적으로 변수를 정의하면 Stack에 저장되고
객체를 인스턴스화하면 Heap에 저장된다.
package ch05;
public class MainTest1 {
// entry
public static void main(String[] args) {
/*
JVM 가상 메모리 공간이 존재한다.
JVM Stack 메모리 공간
Heap 동적 메모리 공간
*/
Book bookBox1 = new Book(); // Heap 이라는 동적 메모리 공간에 존재한다.
int n1 = 10; // Stack 메모리 공간에 존재한다.
} // end of main
}
붕어방틀을 짜듯이 클래스 설계하고
이를 프로그램으로 활용해보자
package ch05;
/*
1. 클래스를 설계하는 코드 측
클래스는 객체를 만들기 위한 설계도면이다.
학생이라는 설계도를 만들 수 있다.
이름, 나이, 성별..
*/
public class Student {
// String <- 문자열 ("홍길동")
String name; // 이름
int grade; // 학년
double height; // 키
double weight; // 몸무게
}
package ch05;
// 2. 코드를 실행하는 측
public class StudentProgram {
// 진입점
public static void main(String[] args) {
int a = 10;
/*
Student 클래스를 메모리에 올려보자
s1이라는 클래스를 '인스턴스화' 했다.
인스턴스화란 메모리에 올리는 행위를 가리킨다.
*/
Student s1 = new Student();
System.out.println("s1: " + s1); // ch05.Student@2f4d3709
System.out.println("a: " + a); // 10
/*
변수의 2가지 종류
기본 데이터 타입 / 참조 데이터 타입
기본 데이터 타입: 상자의 값이 담긴다.
참조 데이터 타입: 주소 값이 담긴다.
*/
} // end of main
}
객체 2강
package ch05;
/*
책이라는 설계도를 구상해보자
컴파일 시점: 코드를 작성하는 시점
런타임 시점: java 명령어를 통해 프로그램이 실행되는 시점
*/
public class Book {
String title;
String author;
int totalPages;
int publishYear;
}
package ch05;
public class BookProgram {
// entry
public static void main(String[] args) {
/*
Book이라는 데이터 타입을 '인스턴스화'하라
new Book();
new 키워드를 써서 메모리에 올리면
인스턴스화 되며
이때 객체라고 부를 수 있다.
*/
Book b1 = new Book();
System.out.println("b1: " + b1); // ch05.Book@2f4d3709
// 참조자료형의 주소값이다
} // end of main
}
객체 3강
package ch05;
/*
User 설계도면 만들기
코드 설계 측
*/
public class User {
String name;
String tell;
String adress;
int age;
String email;
} // end of class
package ch05;
// 코드 실행 측
public class UserProgram {
//entry
public static void main(String[] args) {
/*
사용자 정의 데이터 타입
인스턴스화 해보자(메모리에 올라간다)
-> Heap 이라는 동적 메모리 공간에
*/
User u1 = new User(); // u1에는 주소값이 담긴다
int n1 = 100; // 값이 담긴다
/*
u1과 같은 변수를
참조변수, 레퍼런스타입 등으로 부른다
*/
} // end of main
}
게임에서 전사 클래스를 만든다고 가정하고 코드를 설계해보자
package ch06;
/*
클래스를 설계하는 측
Warrior를 추상화해보자
*/
public class Warrior {
/*
멤버 변수
속성(상태) 설계
멤버 변수를 초기화 하지 않으면
인스턴스화될 때
기본값으로 초기화된다.
*/
String name; // null
int hp; // 0
int power; // 0
int dp; // 0
String color; // null
double weight; // 0.0
boolean isDie; // false
}
package ch06;
/*
코드를 실행하는 측
*/
public class WarriorMainTest1 {
// entry
public static void main(String[] args) {
// 메인 지역
// 지역 변수
Warrior w1 = new Warrior();
w1.name = "아마데우스";
w1.hp = 100;
w1.power = 30;
w1.dp = 10;
w1.color = "빨간색";
// w1 주소값을 이용해 객체의 정보를 출력해보자
System.out.println(w1.color); // 빨간색
System.out.println(w1.name); // 아마데우스
System.out.println("==========");
Warrior w2 = new Warrior();
Warrior w3 = new Warrior();
System.out.println(w2.hp); // 0
System.out.println(w2.weight); // 0.0
System.out.println(w2.isDie); // false
System.out.println(w2.name); // null: 값이 없다
} // end of main
}
또 다른 코드설계 예제
package ch06;
// 코드 설계
public class Bus {
int number;
String type;
String start;
String arrival;
int interval;
}
package ch06;
// 코드 실행
public class BusProgram {
//진입점
public static void main(String[] args) {
Bus b1 = new Bus();
Bus b2 = new Bus();
Bus b3 = new Bus();
b1.number = 2022;
b1.type = "시외";
b1.start = "김해";
b1.arrival = "양산";
b1.interval = 30;
System.out.println(b1.number + "번 " + b1.type + " 버스");
System.out.println(b1.start + " ~ " + b1.arrival + " 운행");
System.out.println("배차 간격은 " + b1.interval + "분");
System.out.println();
System.out.println(b2.number + "번 " + b2.type + " 버스");
System.out.println(b2.start + " ~ " + b2.arrival + " 운행");
System.out.println("배차 간격은 " + b2.interval + "분");
System.out.println();
} // 메인끝
}
코드설계 예제
package ch06;
// 코드 설계
public class Orc {
String name;
String color;
int height;
int weight;
} // end of class
package ch06;
public class OrcMainTest {
// entry
public static void main(String[] args) {
Orc orc1 = new Orc();
Orc orc2 = new Orc();
Orc orc3 = new Orc();
orc1.name = "배드오크";
orc1.color = "초록";
orc1.height = 180;
orc1.weight = 100;
System.out.print(orc1.color + " ");
System.out.println(orc1.name);
System.out.print(orc1.height + " ");
System.out.println(orc1.weight);
System.out.print(orc2.color + " ");
System.out.println(orc2.name);
System.out.print(orc2.height + " ");
System.out.println(orc2.weight);
System.out.print(orc3.color + " ");
System.out.println(orc3.name);
System.out.print(orc3.height + " ");
System.out.println(orc3.weight);
} // end of main
} // end of class
코드설계 예제
package ch06;
//코드 설계
public class Product {
int year;
String level;
String type;
String name;
boolean valid;
}
package ch06;
// 코드 실행
public class ProductProgram {
//entry
public static void main(String[] args) {
Product p1 = new Product();
Product p2 = new Product();
p1.year = 2011;
p1.level = "고급";
p1.type = "텔레비전";
p1.name = "비전와이드";
p1.valid = true;
System.out.print(p1.year + " ");
System.out.print(p1.level + " ");
System.out.println(p1.type);
System.out.print(p1.name + " ");
System.out.println(p1.valid);
System.out.print(p2.year + " ");
System.out.print(p2.level + " ");
System.out.println(p2.type);
System.out.print(p2.name + " ");
System.out.println(p2.valid);
}
}
'Java' 카테고리의 다른 글
| 함수와 메서드 - 함수 (0) | 2025.04.15 |
|---|---|
| 인텔리제이 IntelliJ 편리한 단축키 정리 (계속) (0) | 2025.04.15 |
| 반복문 while (0) | 2025.04.14 |
| 반복문 for (0) | 2025.04.14 |
| 자료의 형 변환 - 강제형변환 (0) | 2025.04.10 |