ABOUT ME

95.12.1 / 취준생 / Java

Today
Yesterday
Total
  • 6. oop1 - (3) PersonDemo (Person객체, 배열)
    java/코드 리뷰 2020. 3. 24. 14:00
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package oop1;
     
    public class Person {
        
        // 멤버 변수(클래스에서 바로 정의한 변수) 정의 -> 객체의 속성이 됨
        String name;
        String tel;
        String email;
        String gender;
        int age;
        // main이 없어서 JVM이 실행 안함
    }
     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    package oop1;
     
    public class PersonDemo1 {
        public static void main(String[] args) {
            
            // Person 객체 만들기
            /*
            Person p1 : Person 설계도로 생성된 객체의 주소값을 담는 변수 p1을 생성
            new Person() : Person 설계도로 메모리에 객체 생성, 생성된 객체의 주소값을 반환
            Person p1 = new Person();
                 : Person 설계도로 메모리에 객체 생성, 생성된 객체의 주소값을 Person 타입 변수 p1에 대입
            */
            Person p1 = new Person();
            Person p2 = new Person();
            Person p3 = new Person();
            
            // p1이 참조하는 객체 속성 변경
            p1.name = "김유신";
            p1.tel = "010-1234-5678";
            p1.email = "kimys@naver.com";
            p1.gender = "남자";
            p1.age = 50;
            // 기존 null, null, null, null, 0 값을 해당 값으로 변경
            
           p2.name = "이순신";
           p2.tel = "010-1234-4321";
           p2.email = "leesh@gmail.com";
           p2.gender = "남자";
           p2.age = 48;
            
           p3.name = "류관순";
           p3.tel = "010-1234-7654";
           p3.email = "ryugs@daum.net";
           p3.gender = "여자";
           p3.age = 17;
            
            
            
        }
    }
     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    package oop1;
     
    public class PersonDemo2 {
        public static void main(String[] args) {
            /*
            Person 객체를 여러 개 담는 배열 사용
            */
            
            // Person 객체 3개 담을 수 있는 배열 생성, people에 대입
    //        int[] num = new int[3];
            Person[] people = new Person[3];
            
            Person p1 = new Person(); // p1은 Person 객체의 주소값을 가짐
            p1.name = "이순신";
            p1.email = "leesh@naver.com";
            p1.age = 60;
            
            Person p2 = new Person(); // p2은 Person 객체의 주소값을 가짐
           p2.name = "류관순";
           p2.email = "ryugs@gmail.com";
           p2.age = 17;
     
            Person p3 = new Person(); // p3은 Person 객체의 주소값을 가짐
           p3.name = "강감찬";
           p3.email = "kang@daum.net";
           p3.age = 80;
            
            // 생성된 각각의 Person 객체 주소값을 배열에 저장
            people[0= p1;
            people[1= p2;
            people[2= p3;
            
            // people 배열에 저장된 모든 Person 객체의 속성을 출력
            
            for(int i=0; i<people.length; i++) {
                Person p = people[i]; // p에 Person 객체의 주소값이 순서대로 대입됨
                System.out.println(p.name + "\t" + p.email + "\t" + p.age);
            }
            
        }
    }
     
Designed by Tistory.