코드가 길어서 선정리 후 문제 좀 하겠다.
처음에 풀 때는 뛰거나, 수영여부를 추상클래스로 하여서 할까도 생각하다가, 가능한 클래스에만 메서드를 정의하는 것으로 했었다.
내가 듣는 강의 선생님의 코드를 보니, interface로 가능 여부를 명세하여 코드를 짰다.
위 요구사항을 만족하는 클래스들을 바탕으로, Main 함수를 다음 동작을 출력( System.out.println )하며 실행하도록 작성하시오. 이동하는
동작은 각자 순서가 맞아야 합니다.
public class Person {
String name;
int age, x, y, speed;
public Person(String name, int age, int x, int y, int speed){
this.name = name;
this.age = age;
this.x = x;
this.y = y;
this.speed = speed;
}
public void walk(int x, int y){
this.x = x;
this.y = y;
getLocation();
}
public void getLocation() {
System.out.println(name + ": (" + x + ", " + y + ")");
}
public void getInformation(){
System.out.println(name);
System.out.println(age);
System.out.println(speed);
System.out.println("(" + x + ", " + y + ")");
}
}
public class GrandParent extends Person{
public GrandParent(String name, int age, int x, int y, int speed) {
super(name, age, x, y, speed);
}
}
public class Parent extends Person{
public Parent(String name, int age, int x, int y, int speed) {
super(name, age, x, y, speed);
}
public void run(int x, int y){
this.speed += 2;
this.x = x;
this.y = y;
getLocation();
}
}
public class Person {
String name;
int age, x, y, speed;
public Person(String name, int age, int x, int y, int speed){
this.name = name;
this.age = age;
this.x = x;
this.y = y;
this.speed = speed;
}
public void walk(int x, int y){
this.x = x;
this.y = y;
getLocation();
}
public void getLocation() {
System.out.println(name + ": (" + x + ", " + y + ")");
}
public void getInformation(){
System.out.println(name);
System.out.println(age);
System.out.println(speed);
System.out.println("(" + x + ", " + y + ")");
}
}
public class Main {
public static void main(String[] args) {
GrandParent grandParent = new GrandParent("aa", 72, 0, 0, 1);
Parent parent = new Parent("bb", 50, 0, 0, 3);
Child child = new Child("cc", 23, 0, 0, 5);
grandParent.getInformation();
parent.getInformation();
child.getInformation();
grandParent.walk(1,1);
parent.walk(1,1);
child.walk(1,1);
parent.run(2, 2);
child.run(2, 2);
child.swim(3, -1);
}
}
public class Human {
String name;
int x, y;
int age;
int speed;
public Human(String name, int x, int y, int age, int speed) {
this.name = name;
this.x = x;
this.y = y;
this.age = age;
this.speed = speed;
}
public Human(String name, int age, int speed){
this(name, 0, 0, age, speed);
}
public void printWhoAmI(){
System.out.println("1)" + name + " 2)" + age + " 3)" + getLocation() + " 4)" + speed);
}
public String getLocation() {
return "(" + x + ", " + y + ")";
}
}
public class GrandParent extends Human implements Walkable{
public GrandParent(String name, int age) {
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(getLocation());
}
}
public class Parent extends Human implements 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(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(getLocation());
}
}
public class Child extends Human implements Walkable, Runnable, Swimmable{
public Child(String name, int age) {
super(name, age, 5);
}
@Override
public void swim(int x, int y) {
printWhoAmI();
System.out.println("swim speed: " + (speed + 1));
this.x = x;
this.y = y;
System.out.println(getLocation());
}
@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(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(getLocation());
}
}
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 Main {
public static void main(String[] args) {
Human grandParent = new GrandParent("aa", 77);
Human parent = new Parent("bb", 49);
Human child = new Child("cc", 24);
Human[] humans = {grandParent, parent, child};
for(Human human:humans){
human.printWhoAmI();
}
System.out.println("시작");
for(Human human:humans){
if (human instanceof Walkable){
((Walkable) human).walk(1, 1);
}
if (human instanceof Runnable){
((Runnable) human).run(2,2);
}
if (human instanceof Swimmable){
((Swimmable) human).swim(3,-1);
}
}
}
}