JAVA/6. Object2
오브젝트 예제 2 (배열활용)
날아올라↗↗
2016. 6. 22. 17:13
728x90
반응형
- package test.com;
- public class Test02Person {
- int height;
- int weight;
- String location;
- String[] friends;
- public Test02Person() {
- height = 187;
- weight = 80;
- location = "Guri";
- friends = new String[3];
- friends[0] = "AA";
- friends[1] = "BB";
- friends[2] = "CC";
- }
- public Test02Person(int height, int weight, String location) {
- this.height = height;
- this.weight = weight;
- this.location = location;
- friends = new String[]{"CHOI", "HAN"};
- }
- public Test02Person(int height, int weight, String location, String[] friends) {
- this.height = height;
- this.weight = weight;
- this.location = location;
- this.friends = friends;
- }
- }
- package test.com;
- public class Test02PersonMain {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println("Person");
- Test02Person p = new Test02Person();
- System.out.println(p.height);
- System.out.println(p.weight);
- System.out.println(p.location);
- System.out.println(p.friends.length);
- System.out.println(p);
- for (int i = 0; i < p.friends.length; i++) {
- System.out.println(p.friends[i]);
- }
- System.out.println("==========================");
- Test02Person p2 = new Test02Person(170, 70, "Gangnam");
- System.out.println(p2.height);
- System.out.println(p2.weight);
- System.out.println(p2.location);
- System.out.println(p2.friends.length);
- System.out.println(p2);
- for (int i = 0; i < p2.friends.length; i++) {
- System.out.println(p2.friends[i]);
- }
- System.out.println("==========================");
- Test02Person p3 = new Test02Person(168, 50, "Guri", new String[]{"KIM","LEE","PARK","PARK2"});
- System.out.println(p3.height);
- System.out.println(p3.weight);
- System.out.println(p3.location);
- System.out.println(p3.friends.length);
- System.out.println(p3);
- for (String friend : p3.friends) {
- System.out.println(friend);
- }
- System.out.println("==========================");
- //p, p2, p3를 한꺼번에 갖는 배열을 작성하시오
- Test02Person[] ps = new Test02Person[3];
- ps[0] = p;
- ps[1] = p2;
- ps[2] = p3;
- //Test02Person[] ps = new Test02Person[]{p, p2, p3};
- System.out.println("키 몸무게 지역 친구수 친구리스트");
- for (int i = 0; i < ps.length; i++) {
- System.out.print(ps[i].height + " ");
- System.out.print(ps[i].weight + " ");
- System.out.print(ps[i].location + " ");
- System.out.print(ps[i].friends.length + " ");
- for (String friend : ps[i].friends) {
- System.out.print("["+friend + "]");
- }
- System.out.println();
- }
- System.out.println();
- } //end main
- } //end class
728x90
반응형