본문 바로가기
프로그램/Java 1000제

Java 18제] 1~10까지 출력하시오.(상속의 메서드 오버라이딩 super 예약어 활용)

by 건티 2022. 11. 30.
728x90

출처 : 반크_세계유산 경복궁

 

참고풀이]

상속할 파일)

public class OneAndTenPaPa {
   int N;//멤버번수
   int S;
   int E;

   public OneAndTenPaPa()
   {
      S=1;
      E=10;
   }

   //멤버 메서드
   public void Show(int N)
   {
      this.N=N;
      for(int i=1; i<=this.N; i++)
         System.out.printf("%3d",i);
      System.out.println();
   }

   public static void main(String[] args) {
      // TODO Auto-generated method stub

   }
}

 

상속 받는 파일)

public class OneAndTenKid3 extends OneAndTenPaPa {

   //멤버 메서드 OverRiding, super예약어 사용
   public void Show(int S, int E)
   {
      super.S=S;
      super.E=E;

      for(int i=super.S; i<=super.E; i++)
         System.out.printf("%3d",i);
      System.out.println();
   }

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      OneAndTenKid3 oatk = new OneAndTenKid3();

      oatk.Show(1,10);

   }

}

 

참고풀이 결과]

 

 

 

 

 

대한민국의 아름다운 영토, 독도의 여름

 

반응형

댓글