-
자바 상속부분과거...../자바 2009. 2. 22. 18:23
자바상속부분부터 오랜만에 다시보게되었다.
중요하게 볼점은 this, super, 기본적인 상속 관계 (extends)다
// shape 클랙스
public class Shape {
private double x,y;
public Shape(double x, double y){
setX(x);
setY(y);
}
public double getX(){
return x;
}
public void setX(double x){
this.x = x;
}
public double getY(){
return y;
}
public void setY(double y){
this.y = y;
}}
//Circle 클래스
public class Circle extends Shape {
private double radius;
public Circle(){
super(0,0);
}
public Circle(double radius){
this(0,0);
this.setRadius(radius);
}
public Circle(double x, double y){
super(x,y);
}
public void draw(){
System.out.println("이 다각형은 원 입니다.");
System.out.println("\t 중심좌표: (" + this.getX() + "," + this.getY() + ")");
System.out.println("t 반지름: " + this.getRadius() + "\n");
}
public double getRadius() {
return radius;
}
public void setRadius(double radius){
this.radius = radius;
}
}
//Polygon 클래스
public class Polygon {
public static void main(String[] args){
Circle poly1 = new Circle();
poly1.draw();
Circle poly2 = new Circle(10, 20);
poly1.draw();Circle poly3 = new Circle(4.5, 7.9);
poly3.draw();Circle poly4 = new Circle(3.5);
poly4.draw();}
}
-오늘할일을 내일로 미루지말자..- 이상