요리보고 조리봐도 모르겠는 부분이 있네요...
영동교
책에서 다형성에 대해 배우는 코너입니다...
일단 코드를 제가 해석해야 진도가 나갈거같은데... 책에는 설명이 없는 부분이 몇가지 있어서 질문올려봅니다~
아래 코드한번 봐주세요~ ㅠㅠ (소설같은 자바 라는 책에 있는 다형성 부분의 예제입니다.)
import java.util.*;
abstract class Employee
{
private String name;
public Employee(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public abstract int getPay();
}
class Permanent extends Employee
{
private int salary;
public Permanent(String name, int salary)
{
super(name);
this.salary=salary;
}
public int getPay()
{
return this.salary;
}
}
class PartTime extends Employee
{
private int time;
private int pay;
public PartTime(String name, int time, int pay)
{
super(name);
this.time=time;
this.pay=pay;
}
public int getPay()
{
return this.time*this.pay;
}
}
class Department
{
private Vector empVector = new Vector();
public void addEmployee(Employee p)
{
this.empVector.add(p);
}
public void showEmployee()
{
for(int i=0;iempVector.size();i++)
{
Employee p = (Employee)empVector.elementAt(i);
System.out.println(p.getName()+:+p.getPay());
}
}
}
public class Main1
{
public static void main(String[] args)
{
Department depart = new Department();
depart.addEmployee(new Permanent(KIM,1000));
depart.addEmployee(new Permanent(LEE,1500));
depart.addEmployee(new PartTime(PARK,10,200));
depart.addEmployee(new PartTime(SONG,10,170));
depart.showEmployee();
}
}
처음 빨간줄 친 (Employee p)같은 경우 Employee 라는 클래스형으로 p를 준다? 이런거로 아는데요.
int 형은 정수, float 는 실수 이런 자료형에 대해서는 알겠는데, 이걸 참조형이라고 하던가요? 이부분에 대한 설명이 미흡해서..
정확히 저 매개변수 괄호안에 들어가는 저부분에 대해서 알고싶어요 ㅠㅠ그리고 두번째 (new Permanent(KIM,1000)); 같은 경우에는 제가 배운건 일반적으로 생성자 만드는
Permanent pe = new Permanent();
가 아닌 괄호안에 new가 붙었는데 이건 어떤의미인가요? 이렇게 new 를 쓰면 생성자의 참조값은 없는게 아닌가요?
이것또한 책을 두권이나 보면서도 설명이 제대로 나와있지가 않아서 여쭈어봅니다~
질문을 하기가 어렵네요... 아이궁... 이거 뭐가이리 어려운건지 ㅠㅠ 답변 감사드립니다~