Java

객체에 값 할당하기

H_u 2024. 4. 15. 17:28
728x90
반응형
SMALL

학습목표

  • 하나의 클래스 설계로 여러 개의 객체를 만들 수 있다
  • 우선 순위가 아주 높은 . 연산자의 이해

 

1. 하나의 클래스 설계로 어려개의 객체를 만들 수 있다

 

package basic.ch05;

 

public class Warrior {

 

//속성

String name;

double height;

double weight;

String color;

int health;

int attackPower;

int defensePower;

 

 

}

 

package basic.ch05;

 

public class WarriorMainTest {

//메인함수 코드의 시작점 --->JVM Stack

public static void main(String[] args) {

 

//new = 키워드 예약어, () <- 생성자

Warrior warrior1 = new Warrior();

//메모리에 올라가면 객체 라고 부른다. heap(동적 메모리 영역)

//객체의 접근은 . 연산자를 통해서 접근할 수 있다

 

Warrior warrior2 = new Warrior();

 

warrior1.name = "티모";

warrior1.health = 100;

warrior1.attackPower = 30;

warrior1.defensePower = 1;

 

System.out.println("-------------------------------");

warrior2.name = "야스오";

warrior2.health = 120;

warrior2.attackPower = 20;

warrior2.defensePower = 2;

 

System.out.println("--------------------------------");

System.out.println(warrior1.name);

System.out.println(warrior2.name);

 

}//end of main

 

}//end of class

 

2. 우선 순위가 아주 높은 . 연산자의 이해

  • 자바 프로그램을 실행 시켰을 때 메모리를 할당 받는다.
  • Heap 메모리 영역에 올라가는 객체에 접근은 . 연산자를 통해 할 수 있다.

728x90
반응형
SMALL

'Java' 카테고리의 다른 글

함수와 만들기 자바 실습 문제  (0) 2024.04.15
함수와 메서드  (0) 2024.04.15
클래스와 객체  (0) 2024.04.15
OOP(객체지향) 란  (0) 2024.04.15
break, continue 사용  (0) 2024.04.12