Point 클래스
public class Point {
private int x;
private int y;
//특별한 이유 없이 default 접근지정자 사용하면 안됨
//멤버 변수는 밑으로 나열하는게 나음
//지역 변수는 옆으로 나열해도 좋다
//기본 생성자 없으면 감점
public Point() {
x = 0;
y = 0;
//기본 생성자 만들어야
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
//c++은 인라인 함수(전처리 하듯이 매크로 확장이 일어나듯이 함수 호출 안일어남)로 처리
public void set(int x, int y) {
//객체에서 호출하므로 public
//protected나 default는 특별한 이유가 있을 떄만
this.x = x;
this.y = y;
}
public void showPoint() {
System.out.println( "("+x+","+y+")");
}
}
ColorPoint 클래스
public class ColorPoint extends Point {
private String sColor;
public ColorPoint() {
sColor = "BLACK";
}
public void setColor(String sColor) {
this.sColor = sColor;
}
//매개 변수와 멤버 변수의 이름이 달라도 되나
//똑같이 쓰는게 이해하기 편리
public void showColorPoint() {
System.out.print(sColor);
showPoint(); //슈퍼클래스에 있는 걸 사용
//1) x,y를 여기 클래스에서 선언 금지
//2) protected 사용 금지
//상속 받은 애가 부모 꺼를 고치는 행위는 있어서는 안됨
}
}
ColorPointEX 클래스
public class ColorPointEx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Point oPoint;
oPoint = new Point();
oPoint.set(1,2);
oPoint.showPoint(); //함수의 시작은 소문자와 동사로 시작
ColorPoint oColorPoint;
oColorPoint = new ColorPoint();
oColorPoint.set(3, 4);
//선언하지 않았음에도 사용 가능
//상속 받았기 때문에 에러 x
oColorPoint.setColor("RED");
oColorPoint.showColorPoint();
}
}