JAVA/chapter11_reference_array
chapter01 : person / personmain
GAWON
2023. 5. 26. 18:17
package org.joonzis.ex;
public class Ex01_Person {
// 필드
String name;
int age;
double height;
char gender;
public Ex01_Person() {}
public Ex01_Person(String name, int age, double height, char gender) {
this.name = name;
this.age = age;
this.height = height;
this.gender = gender;
}
void output() {
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
System.out.println("키 : " + height);
System.out.println("성별 : " + gender);
}
}
package org.joonzis.ex;
public class Ex01_PersonMain {
public static void main(String[] args) {
Ex01_Person person1 = new Ex01_Person("김씨", 20, 130, '남');
Ex01_Person person2 = new Ex01_Person("이씨", 30, 170, '여');
Ex01_Person person3 = new Ex01_Person("박씨", 40, 190, '남');
person1.output();
person2.output();
person3.output();
System.out.println("------------------------");
// 크기 3의 참조 배열 people을 선언 후 값 입력
// 각 배열 내 데이터 출력
Ex01_Person[] people = new Ex01_Person[3];
people[0] = new Ex01_Person("김씨", 20, 130, '남');
people[1] = new Ex01_Person();
for(int i=0; i<people.length; i++) {
people[i] = new Ex01_Person();
}
people[0].output();
}
}