티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/81
Customer customer = new VIPCustomer(); // 묵시적, 업 캐스팅 진행
VIPCustomer vipCustomer = (VIPCustomer) customer; // 명시적, 다운 캐스팅 진행
Customer customer = new Customer(); // 처음부터 상위 클래스로 생성
VIPCustomer vipCustomer = (VIPCustomer) customer; // 다운 캐스팅 불가
좌변객체 instanceof 우변타입
boolean result = customer instanceof VIPCustomer
// customer 객체가 VIPCustomer 클래스의 인스턴스인지, 즉 업 캐스팅이 진행이 되었는지
public class IdolTest {
public static void main(String[] args) {
Idol rIdol = new Rico(); // 형변환
Idol mIdol = new Mari(); // 형변환
Idol yIdol = new Yoshiko(); // 형변환
ArrayList<Idol> idolList = new ArrayList<>();
idolList.add(rIdol);
idolList.add(mIdol);
idolList.add(yIdol);
IdolTest test = new IdolTest();
for(Idol idol : idolList) {
idol.call();
}
test.testDownCasting(idolList);
}
public void testDownCasting(ArrayList<Idol> list) {
for (int i = 0; i<list.size(); i++) {
Idol idol = list.get(i);
if (idol instanceof Rico) {
Rico rIdol = (Rico) idol;
rIdol.raser();
}
else if (idol instanceof Yoshiko) {
Yoshiko yIdol = (Yoshiko) idol;
yIdol.fallen();
}
else if (idol instanceof Mari) {
Mari mIdol = (Mari) idol;
mIdol.money();
}
else {
System.out.println("error");
}
}
}
}
int add(int x, int y); // 선언
int add(int x, int y){} // 정의, 구현부가 있으므로 추상 메소드가 아니며 정의
public abstract class SchoolIdol {
// constructor 생략 (기본 생성자)
// method
abstract void introduce(); // 추상 메소드
abstract void call(); // 추상 메소드
abstract void dancing(); // 추상 메소드
abstract void singing(); // 추상 메소드
}
public class Aqours extends SchoolIdol {
// constructor 생략 (기본 생성자)
// method
@Override
public void introduce() {
System.out.println("안녕하세요 저희는 Aqours 입니다!");
}
@Override
public void call() {
System.out.println("0부터 1에 아쿠아 선샤인!");
}
@Override
public void dancing() {
System.out.println("Aqours가 춤을 춥니다.");
}
@Override
public void singing() {
System.out.println("Aqours가 노래합니다.");
}
}
public class Idol extends Aqours {
// constructor 생략 (기본 생성자)
// method
@Override
public void introduce() {
System.out.println("안녕하세요 아이돌입니다");
}
@Override
public void call() {
System.out.println("열심히 할게요!");
}
@Override
public void dancing() {
System.out.println("아이돌이 춤을 춥니다.");
}
@Override
public void singing() {
System.out.println("아이돌이 노래를 합니다.");
}
public void callRes() { // 업캐스팅 진행 시 사용 불가능하므로 다운캐스팅 필요
System.out.println("아이돌이 콜 앤 리스폰스를 합니다.");
}
}
public class IdolTest {
public static void main(String[] args) {
// Aqours 클래스의 메소드 사용
System.out.println("====Aqours의 자기소개, 구호, 춤추기, 노래하기====");
SchoolIdol schoolIdol = new Aqours(); // 업캐스팅, Aqours > SchoolIdol
schoolIdol.introduce();
schoolIdol.call();
schoolIdol.dancing();
schoolIdol.singing();
// Idol 클래스의 메소드 사용
System.out.println("====아이돌의 자기소개, 구호, 춤추기, 노래하기====");
Aqours aqours = new Idol(); // 업캐스팅, Idol > Aqours
aqours.introduce();
aqours.call();
aqours.dancing();
aqours.singing();
// Idol 클래스에만 있는 메소드 사용
boolean result = aqours instanceof Idol; // instanceof 결과값
// 인스턴스 형태 확인
if (result) {
Idol idol = (Idol) aqours; // 업캐스팅이 된 상태라면 다운캐스팅 진행
idol.callRes(); // Idol 클래스에만 있고 상위 클래스에는 없는 callRes() 메소드 실행
}
}
}
public abstract class Car {
public abstract void drive(); // 추상 메소드
public abstract void stop(); // 추상 메소드
public void startCar() {
System.out.println("시동을 켭니다.");
}
public void turnOff() {
System.out.println("시동을 끕니다.");
}
// 템플릿 메소드
final public void run() {
startCar();
drive();
stop();
turnOff();
}
}
public class AICar extends Car {
@Override // 추상 메소드 재정의
public void drive() {
System.out.println("자율 주행합니다.");
System.out.println("자동차가 스스로 방향을 바꿉니다.");
}
@Override // 추상 메소드 재정의
public void stop() {
System.out.println("스스로 멈춥니다.");
}
}
public class ManualCar extends Car {
@Override // 추상 메소드 재정의
public void drive() {
System.out.println("사람이 운전합니다.");
System.out.println("사람이 핸들을 조작합니다.");
}
@Override // 추상 메소드 재정의
public void stop() {
System.out.println("브레이크를 밟아 정지합니다.");
}
}
public class CarTest {
public static void main(String[] args) {
Car aiCar = new AICar(); // 업캐스팅 진행 AICar > Car
aiCar.run(); // 템플릿 메소드 실행
System.out.println("===============");
Car manualCar = new ManualCar(); // 업캐스팅 진행 ManualCar > Car
manualCar.run(); // 템플릿 메소드 실행
}
}
public static fianl double PI = 3.14;
public class Define {
public static final int MIN = 1;
public static final int MAX = 999999;
public static final double PI = 3.14;
public static final String GREETING = "안녕하십니까 니꼴라스 입니다";
public static final int AQOURS_CODE = 1001;
public static final int LIELLA_CODE = 1002;
}
public class DefineTest {
public static void main(String[] args) {
System.out.println("인사말 : " + Define.GREETING);
System.out.println("최소값 : " + Define.MIN);
System.out.println("최대값 : " + Define.MAX);
System.out.println("Aqours의 코드 : " + Define.AQOURS_CODE );
System.out.println("Liella의 코드 : " + Define.LIELLA_CODE);
System.out.println("원주율 : " + Define.PI);
}
}