Chủ Nhật, 7 tháng 6, 2015

[AP]: Day 4: Abstract Class & Interface

Cũng giống như Java thì trong C# ta có hai thành phần quan trọng nhất của tính thừa kế là Abstract Class và Interface
Abstract Class: Cho phép lớp con định nghĩa lại các ứng xử (behavior) trong khi vẫn cung cấp các tính năng chung cho các lớp con.
Có thể làm cha cho nhiều lớp con nhưng không thể khởi tạo. Có tốc độ nhanh hơn interface. Bổ sung phương thức mới (method) không cần phải thêm vào lớp con

Interface: Được coi là một lớp ảo thuần túy chứa method của các lớp thừa kế nó nhưng không chứa body của các method đó (có nghĩa là không chứa một tý nào code hay bất cứ cái gì khác)
Tuy nhiên có thể cho nhiều lớp bổ sung vào nhưng tốc độ tất nhiên chậm hơn. Bổ sung method mới phải định nghĩa trong lớp con

Lưu ý: Một Class có thể thừa kế nhiều Interface nhưng chỉ có thể nhận một Abstract Class duy nhất
Một so sánh đơn giản về Interface và Abstract Class

Tiếp theo ta sẽ biết thêm về Property và Indexer:
Property: Là một kiểu method đặc biệt giúp dữ liệu được truy cập dễ dàng mà không mất đi sự linh hoạt cũng như an toàn qua các Access Modifier sau: public, protected, private, internal... Còn có tên là Accessor. Có 3 loại gồm Read&Write, Read Only Write Only
Cú pháp: <access modifier>  <return type (Kiều dữ liệu)> <Property name>
{
get{\\return value}
set{\\assign value}
}
Ở đây ta dùng t.Hour để gọi hàm set còn dòng lệnh Writeline để gọi hàm get

Indexer: Là khả năng truy cập vào 1 thành phần của đối tượng giống như truy cập vào mảng
Cú pháp: <access modifier> <return type (Kiểu dữ liệu)> this [<parameter>]
{
get{\\return value}
set{\\assign value}
}
Trong đó bắt buộc phải có tối thiểu 1 parameter và không thể khai báo dưới dạng static (khác với Property)

Homework:
Implement a class named Person and two sub classes of Person named Student and Employee. Make Faculty and Staff sub classes of Employee. A Person has a name, phone number and email address. A student has a program to which he/she enrolled ( Business, Computer Science...) . An Employee has a department, salary and the date hired. A faculty member has office hours and a rank. A staff member has a title. You are required to:
1. Override the ToString() to display the class name and the person's name and email address.
2. Provide properties in each class to read and write it's fields
3. Define a CalculateBonus and CalculateVacation as abstract methods in Employee class and 
implement them in Faculty and Staff as follows
o Faculty get 1000 + 0.05 x Salary and Staff get 0.06 x Salary
o Faculty get 5 weeks if they are employed more than 3 years and additional one week if he/she is "Senior Lecturer". Otherwise 4 weeks. Staff get 4 weeks for 5 year service. 
Otherwise get 3 weeks

Code:
using System;

namespace FinalWork
{
    abstract class Human
    {
        public string name;
        public string phoneNumber;
        public string address;
        public Human(string name, string phone, string add)
        {
            this.name = name;
            this.phoneNumber = phone;
            this.address = add;
        }

        public override string ToString()
        {
            return "Person Info: " + name + "," + phoneNumber + "," + address;
        }
    }

    class Student : Human
    {
        string program;
        public Student(string name ,string phoneNumber,string address,string program) : base (name,phoneNumber,address)
        {
            this.program=program;
        }
       public string display()
        {
            return "Student Info: " + name + "," + phoneNumber + "," + address + "," + program;
        }
    }
    abstract class Employee : Human
    {
        public string department;
        public float salary;
        public string dateHired;
        public int officeHours;
        public int week;
        public Employee(string name, string phoneNumber, string address, string department, float salary, string dateHired, int officeHours,int week) : base (name,phoneNumber,address)
        {
            this.department = department;
            this.salary = salary;
            this.dateHired = dateHired;
            this.officeHours=officeHours;
            this.week=week;
        }
        public abstract void CalculateBonus();
        public abstract void CalculateVacation();
        
    }
    class Faculty : Employee
    {
        string rank;
        public Faculty(string name, string phoneNumber, string address, string department, float salary, string dateHired, int officeHours, int week,string rank):base(name, phoneNumber, address, department, salary, dateHired, officeHours, week)
        {
            
            this.rank = rank;
        }
        public override void CalculateBonus()
        {
            Console.WriteLine(name + "'s Bonus: " + (salary * 0.05 + 1000));
        }
        public override void CalculateVacation()
        {
            if (rank == "Senior Lecturer")
            {
                week = week + 1;
            }
            if (officeHours >= 26208)
            {
                week = week + 5;
            }
            else
            {
                week = week + 4;
            }
            Console.WriteLine(name + "'s Vacation: " + week + " week(s)");
        }
        public string display()
        {
            return "Faculty Info: " + name + "," + phoneNumber + "," + address + "," + department+","+salary + "," + dateHired + "," + officeHours + "," + week+","+","+rank;
        }

    }
    class Staff:Employee
    {
        string title;
        public Staff(string name, string phoneNumber, string address, string department, float salary, string dateHired, int officeHours, int week, string title):base(name, phoneNumber, address, department, salary, dateHired, officeHours, week)
        {
            this.title=title;
        }
        public override void CalculateBonus()
        {
            Console.WriteLine(name + "'s Bonus: " + (salary * 0.06));
        }

        public override void CalculateVacation()
        {
            if (officeHours >= 43680)
            {
                week = week + 4;
            }
            else
            {
                week = week + 3;
            }
            Console.WriteLine(name + "'s Vacation: " + week + " week(s)");
        }
    }
    class run
    {
        static void Main(string[] args)
        {
            Faculty sb = new Faculty("Fernando", "123456789", "alonso@mclaren.com", "Racer", 5000000, "1/1/2015", 58800, 125, "Expert");
            Console.WriteLine(sb.display());
            sb.CalculateBonus();
            sb.CalculateVacation();
            Console.ReadLine();
        }
    }

}

Không có nhận xét nào:

Đăng nhận xét