Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Android
- Spinner
- 오버라이드
- 왕초보
- webpack
- snowpack
- java
- React
- Spring
- 답글
- FIle
- degit
- 게시판
- 코틀린
- parcel
- 쿠키
- 안드로이드
- 스프링
- 스타일보험
- git
- 함수
- 버전일치
- java#왕초보
- kotlin
- 상속
- SQL
- sub query
- 미니게임
- 시큐어코딩
- 숫자
Archives
- Today
- Total
YSHUSH
Inheritance.2 본문
MainClass
public class MainClass {
public static void main(String[] args) {
// Myclass cls = new Myclass();
// Myclass cls1 = new Myclass(222, "홍길동");
// ChildClass cc = new ChildClass(); // 호출순서는 부모클래스가 먼저, 그다음 자식 클래스
ChildClass cc1 = new ChildClass(11, "노지혜", 168.5);
}
}
MyClass
public class Myclass {
private int number;
private String name;
public Myclass() { // 기본 생성자
this(0, "빈칸"); // this(); 는 생성자 맨 윗칸에 적어야 적용할 수 있다!
System.out.println("Myclass() Myclass()");
}
public Myclass(int number, String name) {
// this(); // 기본 생성자
this.number = number;
this.name = name;
System.out.println("Myclass Myclass(int number, String name)");
}
}
ParentClass
public class ParentsClass {
private int number;
private String name;
// public ParentsClass() {
// System.out.println("ParentsClass() ParentsClass()");
// }
public ParentsClass(int number, String name) {
this.number = number;
this.name = name;
}
}
ChildClass
public class ChildClass extends ParentsClass {
private double height;
public ChildClass() {
super(123, "hello"); // super는 부모클래스의 생성자를 알려줌, 생성자 호출시 super와 혼용불가
System.out.println("ChildClass() ChildClass()");
}
public ChildClass(double height) {
super(234, "world");
this.height = height;
}
public ChildClass(int number, String name, double height) {
super(number, name); // super도 맨 위에다 적어줘야 하고 생성자 호출시 this와 혼용불가능
this.height = height;
}
}
'Coding > Java' 카테고리의 다른 글
Over ride.2 (0) | 2021.12.16 |
---|---|
Over ride.1 (0) | 2021.12.16 |
Inheritance.1 (0) | 2021.12.16 |
숫자 정렬 예제 (0) | 2021.12.15 |
Encapsule (0) | 2021.12.14 |