
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | using System; using System.Collections; //Employee 클래스 abstract class Employee { private String name; public Employee(String name) { this.name = name; } public String GetName() { return this.name; } public abstract int GetPay(); } //Permanent 클래스 class Permanent : Employee { private int salary; public Permanent(String name, int salary) : base(name) { this.salary = salary; } public override int GetPay() { return this.salary; } } //PartTime 클래스 class PartTime : Employee { private int time; private int pay; public PartTime(String name, int time, int pay) : base(name) { this.time = time; this.pay = pay; } public override int GetPay() { return this.time * this.pay; } } //Department 클래스 class Department { private ArrayList arList = new ArrayList(); public void AddEmployee(Employee p) { this.arList.Add(p); } public void ShowEmployee() { foreach (Employee p in arList) { Console.WriteLine(p.GetName() + ":" + p.GetPay()); } } } //프로그램 실행 class PolymorphismMain { public static void Main() { 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(); } } /*** KIM:1000 LEE:1500 PARK:2000 SONG:1700 ***/ | cs |
'C# > C# Training' 카테고리의 다른 글
[C#] 배열 (0) | 2018.05.10 |
---|---|
[C#] 다형성 예제 (0) | 2018.05.10 |
[C#] 일형성 (0) | 2018.05.10 |
[C#] Delegate (0) | 2018.05.10 |
[C#] 객체 비교 (0) | 2018.05.02 |