- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter28.java
More file actions
Latest commit
94 lines (80 loc) · 2.41 KB
/
Copy pathChapter28.java
File metadata and controls
94 lines (80 loc) · 2.41 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
packagechapter28;
abstractclassShape{
Stringtype;
Shape (Stringtype){
this.type = type;
}
abstractdoublearea();
abstractdoublelength();
}
classCircleextendsShape{
//원 넓이
//원 둘레
intr;
Circle(intr){
super("원");
this.r = r;
}
@Override
doublearea() {
returnr*r*Math .PI;//호출한 곳으로 돌려준다
}
@Override
doublelength() {
return2*r*Math .PI;
}
publicStringtoString() {
return"Shape [type="+type+", r="+r+"]";
}
}
classRectangleextendsShape{
//사각형 넓이
//사각형 둘레
intwidth, height;
Rectangle(intwidth, intheight){
super("사각형");
this.width = width;
this.height = height;
}
@Override
doublearea() {
returnwidth*height;
}
@Override
doublelength() {
return2*(width+height);
}
publicStringtoString() {
return"Shape [type="+type+", width="+width+", height="+height+"]";
}
}
publicclassShapeEx {
publicstaticvoidmain(String[] args) {
//Circle(int r) 생성자 호출
Circlecir = newCircle(10);
System.out.println(cir);
System.out.println(cir.toString());
System.out.println("반지름 r변수 = "+cir);
System.out.println("원의 넓이"+cir.area());//호출도 하고 결과값도 가지고 있다.
System.out.println("원의 넓이"+cir.length());
System.out.println("----------------------------------------");
//Rectangle(int width, int height) 생성자 호출
Rectanglerect = newRectangle(5,5);
System.out.println(rect);
System.out.println(rect.toString());
System.out.println("사각형 width변수 = "+rect.width);
System.out.println("사각형 heigth변수 = "+rect.height);
System.out.println("사각형의 넓이"+rect.area());//호출도 하고 결과값도 가지고 있다.
System.out.println("사각형의 넓이"+rect.length());
System.out.println("----------------------------------------");
//객체 배열
Shape[] shape = newShape[2];
shape[0] = newCircle(10);
shape[1] = newRectangle(5,5);
for(Shapes : shape) {
System.out.println(s);
System.out.println("넓이 : "+s.area()+"둘레"+s.length());
}
}
}