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

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

by 건티 2022. 11. 21.
728x90

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

 

참고풀이]

상속할 파일)

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

   //멤버 메서드
   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 OneAndTenKid2 extends OneAndTenPaPa {

   int Start, End;

   //멤버 메서드 OverRiding
   public void Show(int Start, int End)
   {
      this.Start=Start;
      this.End=End;

      for(int i=this.Start; i<=this.End; i++)
         System.out.printf("%3d",i);
      System.out.println();
   }


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

      oatk.Show(1,10);

   }

}

 

 

참고파일 결과]

 

 

 

 

 

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

 

반응형

댓글