JAVA에서 상속을 이용한 예제
- OS & Program/JAVA
- 2010. 8. 8. 18:36
*** 검색어: nabiro, JAVA, 상속
[출처] 열혈강의 JAVA Programming / 프리렉(주) / 김승현 저 /
/**
* 상속을 이용한 예제
*/
import java.io.*;
class MyPoint {
private int x;
private int y;
protected static BufferedReader in;
static {
in = new BufferedReader(new InputStreamReader(System.in));
}
protected MyPoint() throws IOException{
System.out.print("x = ");
this.x = Integer.parseInt(in.readLine());
System.out.print("Y = ");
this.y = Integer.parseInt(in.readLine());
}
protected void disp(){
System.out.println();
System.out.println("점(x, y) = (" + this.x + "," + this.y + ")");
}
}
class Circle extends MyPoint{
private int r;
public Circle() throws IOException{
super(); // MyPoint() 호출, 생략 가능
System.out.print("r = ");
this.r = Integer.parseInt(in.readLine());
}
public void disp(){
super.disp();
System.out.println("반지름 r = " + this.r);
}
}
class Rect extends MyPoint{
private int w;
private int h;
public Rect() throws IOException{
super(); // MyPoint() 호출, 생략 가능
System.out.print("w = ");
this.w = Integer.parseInt(in.readLine());
System.out.print("h = ");
this.h = Integer.parseInt(in.readLine());
}
public void disp(){
super.disp();
System.out.println("폭 = " + this.w + ", 높이 " + this.h);
}
}
public class Round13_Ex07 {
public static void main(String[] ar) throws IOException{
Circle cc = new Circle();
cc.disp();
System.out.println();
System.out.println();
Rect rt = new Rect();
rt.disp();
}
}
'OS & Program > JAVA' 카테고리의 다른 글
| ant에서 사용하는 build.xml 파일의 encoding 지정하는 방법 (0) | 2011.01.25 |
|---|---|
| ant 에서 copy 작업에서 디렉터리 제외 시키는 예제 (0) | 2010.12.01 |
| JAVA 에서 this(매개변수) 생성자 예제 (0) | 2010.08.03 |
| JAVA 클래스 생성자에서 this() 생성자 사용시 유의 사항 (0) | 2010.08.03 |
| JAVA class 예제 (0) | 2010.08.03 |