如何用c#编写基于多态子类的数组数据

本文关键字:多态 子类 数据 数组 何用 于多态 | 更新日期: 2023-09-27 18:17:30

我有两种类型的工资单编程工作employee.First是正常员工和salary = base salary + OT wage.Second是项目employee,salary = worked hours * 3000.我已经声明了这样的子类

class Employee
{
    protected string empName;
    protected double pay;
    protected int hour;
    public Employee (string e,int h,double p)
    {
        empName = e; 
        hour = h;
        pay = p;
    }
    public string EmpName
    {
        get { return empName; }
    }
    public int Hour
    {
        get { return hour; }
    }
    public int addHour (int addhour)
    {
        hour += addhour;
        return hour;
    }
    public virtual double Pay(int hour)
    {
        return 0;
    }
}
class Salary : Employee
{
   private double salary;
   public Salary (string e,int h,double p,double s) : base (e,h,p)
    {
        salary = s;
    }
    override public double Pay(int hour)
    {
        double OTWage;
        if (hour <= salary * 0.3)
        {
            OTWage = hour * 120;
        }
        else OTWage = salary * 0.3;
        return salary + wage;
    }
    public double paY
    {
        get { return pay ; }
    }
}
class Project : Employee
{
    private string projName;
    public Project (string e,int h,double p,string proj) : base(e,h,p)
    {
        projName = proj;
    }
    public string ProjName
    {
        get { return projName; }
    }
    override public double Pay(int hour)
    {
        return hour*3000;
    }
    public double paY
    {
        get { return pay; }
    } 
}

和这样的数组

class Program
{
    static void Main(string[] args)
    {
        Salary[] salary = new Salary[100];
        Project[] project = new Project[100];
        salary[0] = new Salary("Safe", 0,10000, 500000);
        project[0] = new Project("Aom", 3,100, "OOP");
        string pname,ename;
        int i,pindex=1,eindex = 1,cont,hour;
        do
        {
            Console.WriteLine("=================Main Menu===============");
            Console.WriteLine("1.Add hours for project employee");
              Console.WriteLine("2.Add overtime hour for salary employee");
            Console.WriteLine("3.Shows total pay for all employees");
            Console.WriteLine("=========================================");
            int choice = Convert.ToInt32(Console.ReadLine());
            if (choice == 1)
            {
                Console.WriteLine("What project do you want to add hour?");
                pname = Convert.ToString(Console.ReadLine());
                for (i = 0; (project[i].ProjName != pname) && i < pindex; i++) ; ;
                if (i < pindex)
                {
                    Console.WriteLine("How much hours do you want to add? ");
                    hour = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Total employee's times for this project is " + project[i].addHour(hour));
                }
            }
            if (choice == 2)
            {
                Console.WriteLine("Who do you want to add overtime hour? ");
                ename = Convert.ToString(Console.ReadLine());
                for (i = 0; (salary[i].EmpName != ename) && i < eindex; i++) ; ;
                if (i < eindex)
                {
                    Console.WriteLine("How much hours do you want to add? ");
                    hour = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Total employee's overtimes is" + salary[i].addHour(hour));
                }
            }
            if (choice == 3)
            {
                Console.WriteLine("=====Payroll Summary=====");  
                Console.WriteLine("You need to pay " + (Project.paY) + " Baht for " + project[0].EmpName);
                Console.WriteLine("You need to pay " + ((Salary)salary[0].Pay(Hour) + " Baht for " + salary[0].EmpName); //something wrong on this part
            }
            Console.WriteLine("Do you want to continue?(yes=1,no=2)");
            cont = Convert.ToInt32(Console.ReadLine());
        }
        while (cont == 1);
        Console.WriteLine("End");

    }
}

}

我不能显示工资摘要,我该怎么办?还是我的程序出了问题?

如何用c#编写基于多态子类的数组数据

这是否解决了您的问题:

        if (choice == 3)
        {
            Console.WriteLine("=====Payroll Summary=====");  
            Console.WriteLine("You need to pay " + (project[0].paY) + " Baht for " + project[0].EmpName);
            Console.WriteLine("You need to pay " + (salary[0].Pay(salary[0].Hour) + " Baht for " + salary[0].EmpName);
        }

如果每个Salary &然后你需要循环遍历每个数组