ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 업 캐스팅?
    Back end/Java 객체지향 2023. 11. 22. 14:21
    public class UpCasting {
      public static void main(String[] args) {
        // 기사 객체 생성 및 초기화
        Knight knight = new Knight();
        knight.name = "아서스";
        knight.hp = 180;
        knight.strength = 50;

        // 도적 객체 생성 및 초기화
        Thief thief = new Thief();
        thief.name = "발리라";
        thief.hp = 120;
        thief.agility = 40;

        // 업 캐스팅 - 부모 타입으로 객체를 해석
        Adventurer adv0 = knight;
        Adventurer adv1 = thief;

        // 모험가들의 배열 생성
        Adventurer[] advs = { adv0, adv1 };
        
        // 모든 모험가의 정보 출력
        for (int i = 0; i < advs.length; i++) {
          System.out.println(advs[i].toString());
        }
      }
    }

    /* 1. 부모 클래스 Adventurer를 만드세요. */
    class Adventurer {
      /* 1.1 공통 필드를 선언하세요. */
        String name;
        int hp;
      
      /* 1.2 공통 메소드를 작성하세요. */
        public void punch() {
        Systehttp://m.out.printf("[%s]의 펀치!!\n", name);
      }

        public String toString() {
        return String.format("[%s] HP: %d", name, hp);
      }

    }

    /* 2. Knight를 Adventurer의 자식 클래스로 정의하세요. */
    class Knight extends Adventurer  {
      /* 2.1 부모와 중복된 필드를 제거하세요. */

      int strength; // 힘

      /* 2.2 부모와 중복된 메소드를 제거하세요. */
      public void berserker() {
        System.out.println("체력과 공격력이 증가합니다.");
      }
    }

    /* 3. Thief를 Adventurer의 자식 클래스로 정의하세요. */
    class Thief extends Adventurer {
      /* 3.1 부모와 중복된 필드를 제거하세요. */
      int agility; // 민첩

      /* 3.2 부모와 중복된 메소드를 제거하세요. */


      public void sharpen() {
        System.out.println("크리티컬 확률이 증가합니다.");
      }
    }

     

    [아서스] HP: 180
    [발리라] HP: 120

     

     

    public class Overriding {
      public static void main(String[] args) {
        // 객체 생성 및 초기화 - 정사각형
        Square s = new Square();
        s.name = "정사각형";
        s.length = 5;
        
        // 객체 생성 및 초기화 - 삼각형
        Triangle t = new Triangle();
        t.name = "삼각형";
        t.base = 4;
        t.height = 3;
        
        // 객체 생성 및 초기화 - 원
        Circle c = new Circle();
        c.name = "원";
        c.radius = 4;

        // 업 캐스팅 - 도형 배열에 정사각형, 삼각형, 원 담기
        Shape[] shapes = { s, t, c };
        
        // 모든 도형의 넓이 계산 및 출력
        for (int i = 0; i < shapes.length; i++) {
          Shape tmp = shapes[i];
          System.out.println("%s의 넓이 -> %.2f\n", tmp.name, tmp.area());
        }
      }
    }

    // 도형
    class Shape {
      String name;

      // 도형의 넓이를 반환
      public double area() {
        return 0;
      }
    }

    // 정사각형
    class Square extends Shape {
      int length; // 한 변의 길이

      /* 1. 정사각형 넓이를 구하도록 area()를 재정의하세요. */
      public double area() {
        return length * length;
      }
      
    }

    // 삼각형
    class Triangle extends Shape {
      int base;   // 밑변
      int height; // 높이

      /* 2. 삼각형 넓이를 구하도록 area()를 재정의하세요. */
      public double area() {
        return base * height / 2.0;
      }
    }

    // 원
    class Circle extends Shape {
      int radius; // 반지름

      /* 3. 원의 넓이를 구하도록 area()를 재정의하세요. */  
      public double area() {
        return Math.PI * radius * radius;
      }
      
      
    }

     

    정사각형의 넓이 -> 25.00
    삼각형의 넓이 -> 6.00
    원의 넓이 -> 50.27

     

    출저: 홍팍 (https://www.youtube.com/@hongpark)

    이 블로그 기록은 개인 공부용 기록입니다.

Designed by Tistory.