Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

Full-Stack 개발자가 되려는 작은 개발자의 블로그

객체 지향 실습2 본문

강의 정리/Java

객체 지향 실습2

jhjin 2020. 4. 1. 16:23

 3. 다형성 
       - 하나의 참조변수로 여러 클래스의 인스턴스를 저장하는 것. 
       - 상속관계에서만 저장이 가능하며, 부모클래스의 참조변수로 자식클래스의 인스턴스를 저장할 수 있음. 
    
       객체의 업캐스팅(Up_casting)과 다운캐스팅(Down_casting) 
           업캐스팅 : 부모클래스의 참조변수에 자손 클래스의 인스턴스를 저장. 
           다운캐스팅 : 부모클래스의 참조변수에 저장된 자손클래스의 인스턴스를 원래 자손 클래스의 참조변수에 복원. 
           instanceof : 업캐스팅된 인스턴스가 어떤 클래스로 만들어져 있는지를 판별하기 위한 명령. 
               ▶ instanceof를 사용하여 인스턴스의 클래스를 구분할 경우 
               ▶ 부모 클래스에 대한 조건을 마지막에 처리해야함. 
               ▶ 먼저 처리할 경우 모든 인스턴스는 부모클래스의 인스턴스에 해당되기 때문에 판별이 안됨. 

        ▶ Object 클래스는 모든 클래스의 조상이기 때문에 Object 참조변수는 모든 인스턴스를 저장할 수 있다! 
        ▶ 기본 자료형(8가지)의 값도 저장할 수 있는데, 
        ▶ 단! Wrapper 클래스를 사용하여 인스턴스로 만들어야 저장할 수 있다!

class Car{
	String color;
	int door;
	
	void drive() {
		System.out.println("간다!");
	}
	void stop() {
		System.out.println("멈춘다!");
	}
}

class FireEngine extends Car{
	void water() {
		System.out.println("물뿌리네...");
	}
}
class Ambulance extends Car{
	void firstAid() {
		System.out.println("사람을 살리네!");
	}
}
class Person{
	
}
class Student extends Person{
	
}
class Researcher extends Person{
	
}
class Professor extends Researcher{
	
}
public class polyTest {
	public static void main(String[] args) {
		FireEngine fe= new FireEngine();
		Ambulance ab=new Ambulance();
		
		//업캐스팅
		Car c1=fe; 
		Car c2=ab;
		
		c1.drive();
		c1.stop();
		//부모 입장에서 자식의 멤버 변수는 없는 것으로 처리
//		c1.water();
		
		//다운캐스팅
		FireEngine fe2=(FireEngine)c1;
		fe2.water();

		System.out.println("==================================");
		Car cars[]=new Car[2];
		cars[0]=fe;
		cars[1]=ab;
		
		area(cars);
		System.out.println("=====================================");
		
		Student st=new Student();
		Researcher rs=new Researcher();
		Professor pf=new Professor();
		
		System.out.print("new Student() > \t");
		printPerson(st);
		System.out.print("new Researcher() > \t");
		printPerson(rs);
		System.out.print("new Professor() > \t");
		printPerson(pf);
	
		
	}
	public static void area(Car c[]) {
		//instanceof를 사용하여 인스턴스를 구분
		for(Car ct : c) {
			if(ct instanceof FireEngine) {
				FireEngine fea=(FireEngine)ct;
				fea.water();
			}
			if(ct instanceof Ambulance) {
				Ambulance aba=(Ambulance)ct;
				aba.firstAid();
			}
		}
	}
	public static void printPerson(Person p) { 
		//판단 조건을 줄 때 조상을 가장 마지막에 판단하는 것이 좋다.
		if(p instanceof Person) {
			System.out.print("Person ");
		}
		if(p instanceof Student) {
			System.out.print("Student ");
		}
		if(p instanceof Researcher) {
			System.out.print("Researcher ");
		}
		if(p instanceof Professor) {
			System.out.print("Professor");
		}
		System.out.println();
	}
}

   

4. 추상화(Abstract)  
        - 미완성 클래스를 작성하는 것. 
        - 개념을 정의한다. 기본(부모/조상) 클래스를 설계할 때 메소드의 이름있는 부분(선언부)만 정의하는 것. 

        추상 클래스(Abstract class) 
             1. 멤버 중에 하나라도 추상 메소드가 있으면 추상 클래스임. 
             2. 추상 클래스 안에는 추상 메소드가 없을 수도 있음. 이 경우 추상 클래스로 만들 필요가 없음. 
        추상 메소드(Abstract method)  
             1. 미완성 메소드 
             2. 선언부만 정의된 메소드. 추상 메소드가 있으면 그 클래스는 반드시 추상 클래스여야 함. 
             3. 추상 메소드는 반드시 상속 받은 하위 클래스에서 내용을 정의해야 함.(또는 하위 클래스도 추상 클래스가 되어야 함.)

package abstractPkg;

abstract class Abs {
	public abstract void absMethod();
}
/*
 * 둘 중 하나를 해야한다.
 * 1.자손 클래스도 abstract로 선언
 * 2.추상 메소드를 재정의 
 */
class ChildAbs extends Abs{
	@Override
	public void absMethod() {
    	System.out.println("틀만 잡힌 메소드를 오버라이딩하여 재정의~~!!")
	}
}

'강의 정리 > Java' 카테고리의 다른 글

객체지향 실습5  (0) 2020.04.18
객체 지향 실습 4  (0) 2020.04.07
물품 관리 프로그램(Homeminus)  (1) 2020.04.01
가계부 프로그램  (0) 2020.03.26
객체 지향 실습3  (0) 2020.03.19
Comments