OLD/자바
Super()-조상 클래스의 생성자
몽블86
2017. 5. 1. 14:52
Object클래스를 제외한 모든 클래스의 생성자 첫 줄에는 생성자 this() 또는 super()를 호출해야 한다. 그렇지 않으면 컴파일러가 자동적으로 super()를 생성자의 첫 줄에 삽입한다.
인스턴스를 생성 할때는 클래스를 선택하는 것만큼 생성자를 선택하는 것도 중요한 일이다.
1.클래스- 어떤 클래스의 인스턴스를 생성할 것인가?
2. 생성자- 선택한 클래스의 어떤 생성자를 이용해서 인스턴스를 생성할 것인가?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class PointTest2 { public static void main(String[] args) { Point3D p3 = new Point3D(); System.out.println("p3.x=" + p3.x); System.out.println("p3.y=" + p3.y); System.out.println("p3.z=" + p3.z); } } class Point { int x = 10; int y = 20; Point(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { int z = 30; Point3D() { this(100, 200, 300); } Point3D(int x, int y, int z) { super(x, y); this.z = z; } } | cs |