JAVA 객체 지향 핵심

(JAVA)객체에 값 할당하기 - 3

mynote6676 2025. 4. 14. 17:46

<aside> 💡 학습목표

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

</aside>

앞에서 우리는 클래스를 설계하고 메모리에 올라갈 수 있도록 하는 연습을 했습니다.

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

 

 

실습 코드

package ch06;
// 1.클래스를 설계하는 측

public class Warrior {

    // 멤버 변수
    //멤버 변수를 초기화 하지 않으면
    //인스턴트화할때
    //기본값으로 초기화 된다.
    //속성(상태) 설계
    String name;
    int hp;
    int power;
    int dp;
    String color;
    double weight;
    boolean isDie;

}//end of class

 

package ch06;

public class WarriorMainTest1 {

    //코드를 실행하는측
    public static void main(String[] args) {
        // 메인 지역
        // 지역 변수
        //int = 10;

        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();
        System.out.println(w2.hp);
        System.out.println(w2.weight);
        System.out.println(w2.isDie);
        //null 값이 없다.
        System.out.println(w2.name);

        Warrior w3 =new Warrior();

    }//end of main
}// end of class

 

 

 

 

 

package ch06;

//코드를 설계하는 측
public class Ork {
    //오크의 이름
    String name;
    //오크의 피부색
    String color;
    //오크의 눈색
    String eye;
    //오크의 벨트
    String belt;
    //오크의 바지
    String pants;
    //오크의 키
    int height;
    //오크의 몸무게
    int weight;

}//end of class

 

package ch06;

//코드를 실행하는 측
public class OrkMainTest {
    public static void main(String[] args) {
        Ork ork1 = new Ork();
        ork1.name = "코코";
         ork1.color = "초록색";
        ork1.eye = "노란색";
        ork1.belt = "갈색";
        ork1.pants = "청바지";
        ork1.height = 190;
        ork1.weight = 120;


        System.out.println(ork1.name);
        System.out.println(ork1.color);
        System.out.println(ork1.eye);
        System.out.println(ork1.belt);
        System.out.println(ork1.pants);
        System.out.println(ork1.height);
        System.out.println(ork1.weight);
    }// end of main
}// end of class
728x90