2024-10-30




x 좌표 >>> 10
y 좌표 >>> 5
z 좌표 >>> 3
package inheritance;
public class Point {
int x;
int y;
public Point() {}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
package inheritance;
/*
* 1. super() 키워드
* - 자식클래스에서 부모클래스의 생성자를 호출하는 명령어.
* 형식) super(인자); // 인자는 생략도 가능함.
*
* 2. this() 키워드
* - 현재(자식) 클래스에서 현제 클래스 안의 다른 생성자를
* 호출하는 명령어.
* 형식) this(인자);
*
* 주의) this() 키워드를 사용 시에는 반드시 생성자 첫 문장에 와야 함.
*/
public class Point3D extends Point {
int z;
public Point3D() {
super(); // 부모클래스의 기본 생성자 호출
}
public Point3D(int x, int y) {
super(x, y); // 부모클래스의 인자 생성자 호출
//this.x = x;
//this.y = y;
}
public Point3D(int x, int y, int z) {
//this.x = x;
//this.y = y;
this(x, y); // 자기자신(자식클래스)의 인자 생성자 호출, 바로 위의 메서드를 호출함.
this.z = z;
}
void print() {
System.out.println("x 좌표 >>> " + x);
System.out.println("y 좌표 >>> " + y);
System.out.println("z 좌표 >>> " + z);
} // print() 메서드 end
}
package inheritance;
public class Point_04 {
public static void main(String[] args) {
Point3D point = new Point3D(10, 5, 3);
point.print();
}
}'Java > 기초 내용 정리' 카테고리의 다른 글
| Java(Class2)_Override_01 (0) | 2024.10.30 |
|---|---|
| Java(Class2)_Inheritance_05 (0) | 2024.10.30 |
| Java(Class2)_Inheritance_03 (0) | 2024.10.30 |
| Java(Class2)_Inheritance_02 (0) | 2024.10.30 |
| Java(Class2)_Inheritance_01 (0) | 2024.10.30 |