<Point.java>
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package ex02.override;
public class Point { // super class
protected int x, y;
//protected: 보호모드, 상속받은 자손들 접근 가능
//생성자함수 2개 이상
public Point() {
x=y=50;
System.out.println("Point daeault 생성자함수");
}
public Point(int x, int y) {
this.x = x;
this.y = y;
System.out.println("Point 매개변수2개 생성자함수");
}
public Point(int x) {
this.x = x;
System.out.println("Point 매개변수1개 생성자함수");
}
//getter, setter method
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//toString() ->sub class에 써도 가능, display()를 쓰지 않아도 됨
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
//output method
// public void display() { //=>override(재정의) method라고 함
// //함수명, 매개변수 개수, 리턴 타입이 같은 경우
// System.out.println("x = " + x + "\ty = " + y);
// }
}
|
cs |
▶출력하기 위한 메소드 만드는 방법2가지
1.toString(): 자동생성가능한 내장 메소드
Circle c = new Circle();
System.out.println(c);
->toString()를 불러오지 않아도 됨
2.display(): 내가 만든 메소드
Circle c = new Circle();
c.display();
->display()를 불러와야 함
<Circle.java>
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package ex02.override;
public class Circle extends Point { // sub class
private int r;
//생성자함수
public Circle() {
System.out.println("Circle daeault 생성자함수");
r = 5;
}
public Circle(int r) {
System.out.println("Circle 매개변수1개 생성자함수");
this.r = r;
}
public Circle(int x, int y, int r) {
super(x,y);
this.r = r;
System.out.println("Circle 매개변수3개 생성자함수");
}
//getter, setter method
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
//toString() 모습 2), display()를 쓰지 않아도 됨
@Override
public String toString() {
return "Circle [r=" + r + ", x=" + x + ", y=" + y + "]";
}
//toString() 모습 1), display()를 쓰지 않아도 됨
// @Override
// public String toString() {
// super.toString();
// return "Circle [r=" + r + "]";
// }
// output method
// public void display() { //=>override(재정의) method라고 함
// //함수명, 매개변수 개수, 리턴 타입이 같은 경우
// System.out.println("x = " + getX() + "\ty = " + getY() + "\tr = " + r);
// }
}
|
cs |
▶toString()의 모습 2가지
1-1)@Override
public String toString() {
super.toString(); ->부모 클래스에서 만들 toString() 불러와서 출력 후
return "Circle [r=" + r + "]"; ->해당 클래스에서 출력할 내용 return
}
1-2)@Override
public String toString() {
return "Circle [r=" + r + ", x=" + x + ", y=" + y + "]";
}
▶override(재정의)
-함수명, 리턴타입, 매개변수 개수가 같은 함수
cf)overload?
<Rectangle.java>
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package ex02.override;
public class Rectangle extends Point { // sub class
private int x2, y2;
//생성자 함수
public Rectangle() {
System.out.println("Circle daeault 생성자함수");
x2 = y2 = 70;
}
public Rectangle(int x2, int y2) {
this.x2 = x2;
this.y2 = y2;
System.out.println("Point 매개변수2개 생성자함수");
}
public Rectangle(int x, int y, int x2, int y2) {
super(x,y);
this.x2 = x2;
this.y2 = y2;
System.out.println("Point 매개변수4개 생성자함수");
}
//getter, setter method
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
//toString() ->sub class에 써도 가능, display()를 쓰지 않아도 됨
@Override
public String toString() {
return "Rectangle [x2=" + x2 + ", y2=" + y2 + ", x=" + x + ", y=" + y + "]";
}
// output method
// public void display() {
// System.out.println("x = " + getX() + "\ty = " + getY()
// + "\tx2 = " + x2 + "\ty2 = " + y2);
// }
}
|
cs |
<MainEntry.java>
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
|
package ex02.override;
public class MainEntry {
public static void main(String[] args) {
Circle c = new Circle(); //만약 매개변수가 없는 Circle()생성자가 없다면 오류날것
c.setR(10);
c.setX(100);
c.setY(20);
System.out.println(c); //toString()을 사용해서 객체 c만 출력해도 가능
//c.display();
System.out.println("================================");
Rectangle r = new Rectangle();
r.setX(5);
r.setX2(6);
r.setY(7);
r.setY2(8);
System.out.println(r); //toString()을 사용해서 객체 c만 출력해도 가능
//r.display();
}
}
|
cs |
Point daeault 생성자함수
Circle daeault 생성자함수
Circle [r=10, x=100, y=20]
================================
Point daeault 생성자함수
Circle daeault 생성자함수
Rectangle [x2=6, y2=8, x=5, y=7]
'JAVA > 12_inheritance' 카테고리의 다른 글
super instance로 sub class 객체 생성 가능, super instance = new sub() (0) | 2021.03.07 |
---|---|
super와 super() (0) | 2021.01.21 |
inheritance - private와protected,super,input,this.r과setR(),생성자 (0) | 2021.01.21 |
super()와 this() 모두 생성자 맨앞에 위치해야 한다 (0) | 2021.01.20 |
inheritance - <<2.이클립스 기반 class 만들어 상속>> + super() + this (0) | 2021.01.20 |