PohamMain
package pack.your;
public class PohamMain {
public static void main(String[] args) {
PohamCar kildong = new PohamCar("길동");
kildong.turnHandle(30);
System.out.println(kildong.ownerName + "의 회전량은 " + kildong.turnShow + kildong.handle.quantity); // kildong.handle.quantity 포함관계로 접근한 경우
PohamCar sunsin = new PohamCar("순신");
sunsin.turnHandle(-20);
System.out.println(sunsin.ownerName + "의 회전량은 " + sunsin.turnShow + sunsin.handle.quantity); // kildong.handle.quantity 포함관계로 접근한 경우
}
}
=====================================================================================================================
PohamHandle
package pack.your;
public class PohamHandle {
int quantity;
public PohamHandle(){
quantity = 0;
}
String leftTurn(int q){
quantity = q;
return "좌회전";
}
String rightTurn(int q){
quantity = q;
return "우회전";
}
String straight(int q){
quantity = q;
return "직진";
}
}
=====================================================================================================================
PohamCar
package pack.your;
public class PohamCar {
String ownerName, turnShow;
PohamHandle handle; // 클래스의 포함 - 필드처럼 사용
public PohamCar() {
}
public PohamCar(String name) {
ownerName = name;
handle = new PohamHandle();
}
void turnHandle(int q){
if(q > 0) turnShow = handle.rightTurn(q);
if(q < 0) turnShow = handle.leftTurn(q);
if(q == 0) turnShow = handle.straight(q);
}
}
'자바' 카테고리의 다른 글
내부클래스 (0) | 2014.02.21 |
---|---|
인터페이스와 어댑터(추상클래스) (0) | 2014.02.21 |
Singleton 이해하는 예제 (0) | 2014.02.19 |
업캐스팅 / 다운캐스팅 - 참조변수의 형변환 instanceof (0) | 2014.01.19 |
getter와 setter, 생성자의 접근 제어자 (0) | 2014.01.19 |