Java/Overriding
super 연습문제 (에러 찾기)
Bohemian life
2012. 4. 11. 16:49
package com.over3;//super에러 해결하기 public class PointTest { public static void main(String[] args){ Point3D p3 = new Point3D(1,2,3); System.out.println(p3.getLocation()); } } //Point(){} class Point{ int x; int y; String getLocation(){ return "x :"+x+", y :"+y; } } class Point3D extends Point{ int z; Point3D(int x, int y, int z){ /*2.에러 해결방법 부모 클래스에 default 생성자가 없고 인자를 전달해야 하는 생성자만 있을경우 명시적으로 super(인자)를 이용해서 부모클래스 생성자 호출 super(x,y);*/ this.x = x; this.y = y; this.z = z; } String getLocation(){//오버라이딩 return "x : " +x+", y :"+y+"m z :" + z; } }
x : 1, y :2m z :3