본문 바로가기

JAVA/DAY 15 _ 23.09.06

상속_array활용3

// Person 및 하위 클래스 정의

class Occupation{
	
	private String name;
	
	public Occupation(String name){
		
		this.name = name;
	}
	
	// 오버라이딩할 메서드
	public String print() {

		return "이름 : " + name;
	}
}
class Student extends Occupation{
	
	
	private String doWhat;
	
	public Student(String name, String doWhat) {
		super(name);
		this.doWhat = doWhat;

	}
	

	public String print() {

		return super.print() + ", 하는일 : " + doWhat;
	}

}


class Nurse extends Occupation{
	
	private String major;
	
	Nurse(String name, String major){
		super(name);
		this.major = major;
	}
	

	public String print() {

		return super.print() + ", 전공 : " + major;
	}
	
	
}

 

 

// Main

ccupation[] job = new Occupation[3];

    job[0] = new Student("민지", "코딩");
    job[1] = new Nurse("이민지", "간호학과");
    job[2] = new Student("미인지", "공부");


    for(Occupation arr : job) {
        System.out.println(arr.print());
    }

'JAVA > DAY 15 _ 23.09.06' 카테고리의 다른 글

Abstract Class_ 스타ex활용  (0) 2023.09.14
Overiding  (0) 2023.09.13
상속_array활용2  (0) 2023.09.12
상속_array활용  (0) 2023.09.12
Object class  (0) 2023.09.12