๋ค์์ ์์ ๊ด๊ณ๋ฅผ ๊ฐ๋ ํด๋์ค๋ค์ ์์ฑํ์ธ์. ํนํ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ ๊ท์น๊ณผ, super() ๋ฐ super๋ฅผ ์ ์ฉํด์ ์ค๋ณต์ด ์ต์ํ๋๋๋ก ์์ฑํ์ธ์.
Shape – Line - Square – Rect
– Circle
1) Shape
- ๋ฉค๋ฒ๋ณ์: String name
- ์์ฑ์: name์ ์ด๊ธฐํ
- ๋ฉ์๋: void printInfo() //name์ ์ถ๋ ฅ
2) Line
- ๋ฉค๋ฒ๋ณ์: int length
- ์์ฑ์: name๊ณผ length๋ฅผ ์ด๊ธฐํ
- ๋ฉ์๋: void printInfo() //name๊ณผ length๋ฅผ ์ถ๋ ฅ
3) Square
- ๋ฉค๋ฒ๋ณ์: int area;
- ์์ฑ์: name๊ณผ length๋ฅผ ์ด๊ธฐํ
- ๋ฉ์๋: void getArea(int, int) //์ฌ๊ฐํ์ ๋์ด๋ฅผ ๊ตฌํจ=> area = length * length
void printInfo() //name, length, area๋ฅผ ์ถ๋ ฅ
3) Rect
- ๋ฉค๋ฒ๋ณ์: double length2
- ์์ฑ์: name๊ณผ length, length2๋ฅผ ์ด๊ธฐํ
- ๋ฉ์๋: void printInfo() //name, length, length2, area๋ฅผ ์ถ๋ ฅ
4) Circle
- ๋ฉค๋ฒ๋ณ์: double r, area;
- ์์ฑ์: name๊ณผ r์ ์ด๊ธฐํ
- ๋ฉ์๋: void getArea() //์์ ๋์ด๋ฅผ ๊ตฌํจ=> area = 3.14 * r * r
OverridingEx ํด๋์ค
1) main()
- Line, Square, Rect, Circle ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ,
์ด ๊ฐ์ฒด๋ค์ ๋งค๊ฐ๋ณ์๋ก printํธ์ถ
2) void print(Shape s)
- ๋งค๊ฐ๋ณ์๋ก ๋์ด์จ ๊ฐ์ฒด์ ํ์
์ ํ์ธํ๊ณ , ์๋์ ์กฐ๊ฑด๋๋ก ์ฒ๋ฆฌ(๋ค์ด ์บ์คํ
ํ์)
* Line: name, length ์ถ๋ ฅ
* Square: name๊ณผ length, area ์ถ๋ ฅ
* Rect: name๊ณผ length, length2, area ์ถ๋ ฅ
* Circle: name๊ณผ r, area ์ถ๋ ฅ
class Shape{
String name; //๋ฉค๋ฒ๋ณ์
Shape(String name){
this.name = name; //name์ ์ด๊ธฐํ
}
void printInfo() { //name์ ์ถ๋ ฅ
System.out.println(name);
}
}
class Line extends Shape{
int length; //๋ฉค๋ฒ๋ณ์
Line(String name, int length){ //name, length ์ด๊ธฐํ
super(name);
this.length = length;
}
@Override
void printInfo() { //name, length ์ถ๋ ฅ
super.printInfo();
System.out.println("length: "+length);
}
}
class Square extends Line{
int area; //๋ฉค๋ฒ๋ณ์
Square(String name, int length){
super(name, length);
getArea(length, length); //๋์ด ๊ณ์ฐ
}
void getArea(int w, int h) { //์ฌ๊ฐํ ๋์ด ๊ตฌํ๋ ๋ฉ์๋
length = w;
area = length * length;
}
@Override
void printInfo() { //name, length, area ์ถ๋ ฅ
super.printInfo();
System.out.println("area: "+area);
}
}
class Rect extends Square{
int length2; //๋ฉค๋ฒ๋ณ์
Rect(String name, int length, int length2){ //name, length, length2 ์ด๊ธฐํ
super(name, length);
this.length2 = length2;
getArea(length, length2); //๋์ด ๊ตฌํ๊ธฐ ํธ์ถ
}
@Override
void getArea(int w, int h) { //์ฌ๊ฐํ ๋์ด ๊ตฌํ๊ธฐ. ๋ด์ฉ(super)์ ์์ ํ ์ญ์ ํ๊ณ ๋ค์ ์จ๋ ๋จ.
length = w;
length = h;
area = length * length2;
}
@Override
void printInfo() { //name, length, length2, area ์ถ๋ ฅ
super.printInfo();
System.out.println("length2: "+length2);
}
}
class Circle extends Shape{
double r, area; //๋ฉค๋ฒ๋ณ์
Circle(String name, double r){ //name, r ์ด๊ธฐํ
super(name);
this.r = r;
}
void getArea() { //๋์ด ๊ตฌํ๊ธฐ. ๋ถ๋ชจ์ ํฌํจ๋์ด์์ง ์์ ์๋ก ๋ง๋ฆ
area = 3.14 * r * r;
}
@Override
void printInfo() { //์ถ๋ ฅ
super.printInfo();
getArea(); //๋งค๊ฐ๋ณ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ด๊ณณ์์ ๋์ด ๊ตฌํ๊ธฐ ํธ์ถํด๋ ๊ด์ฐฎ์
System.out.println("r: "+r+" area: "+area);
}
}
public class OverridingEx {
static void print(Shape obj) { //์
์บ์คํ
if(obj instanceof Shape) {
obj.printInfo();
}else if(obj instanceof Line) {
Line l = (Line)obj; //๋ค์ด์บ์คํ
l.printInfo();
}else if(obj instanceof Square) {
Square sq = (Square)obj; //๋ค์ด์บ์คํ
sq.printInfo();
}else if(obj instanceof Rect) {
Rect r = (Rect)obj; //๋ค์ด์บ์คํ
r.printInfo();
}else if(obj instanceof Circle) {
Circle c = (Circle)obj; //๋ค์ด์บ์คํ
c.printInfo();
}else {
System.out.println("๊ทธ๋ฐ ๋ํ์ ์์ต๋๋ค.");
}
System.out.println("---------------");
}
public static void main(String[] args) {
Shape s = new Shape("Shape");
Line l = new Line("Line", 10);
Square sq = new Square("Square", 20);
Rect r = new Rect("Rect", 10, 20);
Circle c = new Circle("Circle",2.0);
print(s); print(l); print(sq); print(r); print(c);
sq.getArea(2, 3);
sq.printInfo();
r.getArea(3, 4);
r.printInfo();
}
}