2012년 12월 29일 토요일

[Ch10Ex07] 상속관계의 생성자 문제

Result :


 Point2D 생성자

  x  grid  : 100
  y  grid  : 200
  z  grid  : 300



Sample Code

File : Point2D


package kame.ch10.ex07;

public class Point2D {
   protected int x;
   protected int y;
 
   public Point2D( ){
  System.out.println( " Point2D 생성자 ");
   }
 
   public Point2D(int x, int y){
  this.x=x;
  this.y=y;
   }
}


File :  Point3D


package kame.ch10.ex07;

public class Point3D extends Point2D {
protected int z=30;

public Point3D( ){
super(0, 0);
System.out.println( " Point3D 생성자 ");
}

public Point3D(int x, int y, int z){
     super(x, y);
 this.x=x;
 this.y=y;
 this.z=z;
   }

public void showPoint( ){
System.out.println( "  x  grid  : " + x );
System.out.println( "  y  grid  : " + y );
   System.out.println( "  z  grid  : " + z );
}
}

File : Ch10Ex07


import kame.ch10.ex07.Point3D;

public class Ch10Ex07 {
  public static void main(String[] args) {
Point3D  pt = new Point3D(100, 200, 300);
pt.showPoint( );
  }
}


Applied Code 1

Result :

  x  좌표 : 100
  y  좌표 : 200
  z  좌표 : 300




File : Ch10Ex07

import kame.ch10.ex07.Point3D;

public class Ch10Ex07 {
  public static void main(String[] args) {
Point3D  pt = new Point3D(100, 200, 300);
pt.showPoint( );
  }
}


File : Point2D


package kame.ch10.ex07;

public class Point2D {
   protected int x;
   protected int y;
 
//   public Point2D( ){
//   System.out.println( " Point2D 생성자 ");
//   }
 
   public Point2D(int x, int y){
  this.x=x;
  this.y=y;
   }
}


File : Point3D



package kame.ch10.ex07;

public class Point3D extends Point2D {
protected int z=30;

public Point3D( ){
super(0, 0);
System.out.println( " Point3D 생성자 ");
}

public Point3D(int x, int y, int z){
          super(x, y);
 this.x=x;
 this.y=y;
 this.z=z;
   }

public void showPoint(){
            System.out.println( "  x  좌표 : " + x );
    System.out.println( "  y  좌표 : " + y );
   System.out.println( "  z  좌표 : " + z );
}
}

Applied Code 2
the result is the same
File : No Point2D. java

File :  Point3D


package kame.ch10.ex07;

public class Point3D {
protected int x=30;
protected int y=30;
protected int z=30;


public Point3D(int x, int y, int z){
 this.x=x;
 this.y=y;
 this.z=z;
   }

public void showPoint(){
System.out.println( "  x  좌표 : " + x );
System.out.println( "  y  좌표 : " + y );
        System.out.println( "  z  좌표 : " + z );
}
}



File : Ch10Ex07



import kame.ch10.ex07.Point3D;

public class Ch10Ex07 {
  public static void main(String[] args) {
Point3D  pt = new Point3D(100, 200, 300);
pt.showPoint( );
  }
}


















Reference book 이클립스로 배우는 Java 6 Programming/ 임종권, 전병찬/ 가메출판사


댓글 없음:

댓글 쓰기