Java - 객체(2)

2022. 4. 25. 17:18Java

객체.zip
0.00MB

 

 

클래스

public class People {
    int age;
    String name;
    int x, y;
    int speed;
        public People(String name, int age, int spped, int x, int y){ //이름 나이 속도와 좌표를 입력받는 생성자
            this.name = name;
            this.age = age;
            this.speed = spped;
            this.x = x;
            this.y = y;
        }
        public People(String name, int age, int speed){ //이름 나이 속도만 입력해서 생성할경우 기본 좌표 0,0
            this(name, age, speed, 0, 0);
        }
        public String getLocation(){ //현재 위치 출력하는 메소드
            return "("+x+","+y+")";
        }
        public void printWhoAmI(){ //현재 내가 누구인지 출력하는 메소드
            System.out.println("My name is "+ name + " age:" +age);
        }


}
public interface Walkable{
    void walk(int x, int y);
}
public interface Runnable {
    void run(int x, int y);
}
public interface Swimmable {
    void swim(int x, int y);
}
public class GrandParent extends People implements Walkable{
//GrandParent 클래스는 People의 멤버들을 상속받고 선언만한 Walkable을 오버라이딩하여 사용하겠다.
    public GrandParent(String name, int age) { //이름과 나이만 입력하여 생성하고 기본속도는 1
        super(name, age,1);
    }

    @Override
    public void walk(int x, int y) {
        printWhoAmI(); 
        System.out.println("walk speed: "+speed);
        this.x=x;
        this.y=y;
        System.out.println("walked to "+getLocation());
    }
}
public class Parent extends People implements Walkable, Runnable{
//Parent 클래스는 People의 멤버들을 상속받고 Walkable, Runnable을 오버라이딩하여 사용    
    public Parent(String name, int age) {
        super(name, age, 3);
    }

    @Override
    public void run(int x, int y) {
        printWhoAmI();
        System.out.println("run speed: "+(speed+2));
        this.x=x;
        this.y=y;
        System.out.println("ran to "+getLocation());
    }

    @Override
    public void walk(int x, int y) {
        printWhoAmI();
        System.out.println("walk speed: "+speed);
        this.x=x;
        this.y=y;
        System.out.println("walked to "+getLocation());
    }
}
public class Child extends People implements Walkable, Runnable, Swimmable{
//Child 클래스는 People의 멤버들을 상속받고 Walkable, Runnable, Swimmable을 오버라이딩하여 사용    
    public Child(String name, int age) {
        super(name, age, 5);
    }

    @Override
    public void run(int x, int y) {
        printWhoAmI();
        System.out.println("run speed: "+(speed+2));
        this.x=x;
        this.y=y;
        System.out.println("Ran to "+getLocation());
    }

    @Override
    public void swim(int x, int y) {
        printWhoAmI();
        System.out.println("swimming speed: "+(speed+1));
        this.x=x;
        this.y=y;
        System.out.println("swum to "+getLocation());
    }

    @Override
    public void walk(int x, int y) {
        printWhoAmI();
        System.out.println("walk speed: "+speed);
        this.x=x;
        this.y=y;
        System.out.println("walked to "+getLocation());
    }
}
public class Main {

    public static void main(String[] args) {
        People grandParent = new GrandParent("할아버지",70); //GrandParent 객체 생성
        People parent = new Parent("엄마",50);//Parent 객체 생성
        People child = new Child("나",20);//Child 객체 생성

        People[] peoples = {grandParent,parent,child}; //객체 배열 peoples 선언
        for(People people:peoples){ //배열 안의 요소들을 모두 출력
            System.out.println(people.name+ ",나이 "+people.age+",속도 "+people.speed+",위치 "+people.getLocation());
        }
        System.out.println("********활동시작******** ");
        for(People people:peoples){
            if (people instanceof Walkable) {
                //instanceof 연산자는 객체가 어떤 클래스인지, 상속받은 클래스가 어떤 것인지 확인할때 사용
                //people이 Walkable을 상속 받았을때
                ((Walkable) people).walk(1, 1);
                System.out.println(" - - - - - - ");
            }
            if (people instanceof Runnable) {
                //people이 Runnable을 상속 받았을때
                ((Runnable) people).run(2, 2);
                System.out.println(" - - - - - - ");
            }
            if (people instanceof Swimmable) {
                //people이 Swimmable을 상속 받았을때
                ((Swimmable) people).swim(3, -1);
                System.out.println(" - - - - - - ");
            }

        }
    }
}

 

 

출력 결과

할아버지,나이 70,속도 1,위치 (0,0)
엄마,나이 50,속도 3,위치 (0,0)
나,나이 20,속도 5,위치 (0,0)
********활동시작******** 
My name is 할아버지 age:70
walk speed: 1
walked to (1,1)
 - - - - - - 
My name is 엄마 age:50
walk speed: 3
walked to (1,1)
 - - - - - - 
My name is 엄마 age:50
run speed: 5
ran to (2,2)
 - - - - - - 
My name is 나 age:20
walk speed: 5
walked to (1,1)
 - - - - - - 
My name is 나 age:20
run speed: 7
Ran to (2,2)
 - - - - - - 
My name is 나 age:20
swimming speed: 6
swum to (3,-1)
 - - - - - - 

Process finished with exit code 0

'Java' 카테고리의 다른 글

Java - 패키지와 모듈화 / Wrapper  (0) 2022.06.21
Java - 상속/인터페이스/추상클래스  (0) 2022.06.21
Java - 클래스와 객체  (0) 2022.06.20
Java - 컬렉션  (0) 2022.06.19
Java - 객체(1)  (0) 2022.04.21