Administrator
发布于 2025-10-14 / 2 阅读
0
0

多态虚方法重写练习题

练习题

练习1:找错误

找错误1: 重写基类方法

public class BaseClass
{
    public void ShowMessage() 
    {
        Console.WriteLine("基类消息");
    }
}
​
public class DerivedClass : BaseClass
{
    public override void ShowMessage() 
    {
        Console.WriteLine("派生类消息");
    }
}

找错误2: 重写基类方法

public class BaseClass
{
    public virtual void ShowMessage()
    {
        Console.WriteLine("基类消息");
    }
}
​
public class DerivedClass : BaseClass
{
    public void ShowMessage() 
    {
        Console.WriteLine("派生类消息");
    }
}

找错误3:重写基类方法

public class Animal
{
    public virtual void Speak() { }
}
public class Dog : Animal
{
    private override void Speak() { }
}

练习2:交互式员工管理系统

虚方法重写练习,具有以下功能:

  1. 添加全职员工 - 输入姓名和月薪

  2. 添加兼职员工 - 输入姓名、工作小时和时薪

  3. 查看所有员工信息 - 显示所有已添加员工的基本信息

  4. 计算所有员工工资 - 计算并显示每个员工的工资及总工资

  5. 演示多态性 - 展示如何使用基类引用调用不同派生类的方法

  6. 退出系统

using System;
​
namespace InteractiveEmployeeSystem
{
    // 基类:员工
    class Employee
    {
        public string Name { get; set; }
        
        // 虚方法:计算工资
        public virtual double CalculateSalary()
        {
            return 0;
        }
        
        // 虚方法:显示信息
        public virtual void DisplayInfo()
        {
            Console.WriteLine($"员工: {Name}");
        }
    }
    
    // 全职员工类
    class FullTimeEmployee : Employee
    {
        public double MonthlySalary { get; set; }
        
        public override double CalculateSalary()
        {
            return MonthlySalary;
        }
        
        public override void DisplayInfo()
        {
            Console.WriteLine($"全职员工: {Name}, 固定月薪: {MonthlySalary}元");
        }
    }
    
    // 兼职员工类
    class PartTimeEmployee : Employee
    {
        public int WorkHours { get; set; }
        public double HourlyRate { get; set; }
        
        public override double CalculateSalary()
        {
            return WorkHours * HourlyRate;
        }
        
        public override void DisplayInfo()
        {
            Console.WriteLine($"兼职员工: {Name}, 工作小时: {WorkHours}, 时薪: {HourlyRate}元");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== 交互式员工管理系统 ===");
            Console.WriteLine("练习虚方法重写和多态性");
            Console.WriteLine();
            
            bool running = true;
            
            while (running)
            {
                Console.WriteLine("请选择操作:");
                Console.WriteLine("1. 添加全职员工");
                Console.WriteLine("2. 添加兼职员工");
                Console.WriteLine("3. 查看所有员工信息");
                Console.WriteLine("4. 计算所有员工工资");
                Console.WriteLine("5. 演示多态性");
                Console.WriteLine("6. 退出");
                Console.Write("请输入选择 (1-6): ");
                
                string choice = Console.ReadLine();
                Console.WriteLine();
                
                switch (choice)
                {
                    case "1":
                        AddFullTimeEmployee();
                        break;
                    case "2":
                        AddPartTimeEmployee();
                        break;
                    case "3":
                        ShowAllEmployees();
                        break;
                    case "4":
                        CalculateAllSalaries();
                        break;
                    case "5":
                        DemonstratePolymorphism();
                        break;
                    case "6":
                        running = false;
                        Console.WriteLine("感谢使用员工管理系统,再见!");
                        break;
                    default:
                        Console.WriteLine("无效选择,请重新输入。");
                        break;
                }
                
                Console.WriteLine();
            }
        }
        
        // 存储员工的数组
        static Employee[] employees = new Employee[10];
        static int employeeCount = 0;
        
        // 添加全职员工
        static void AddFullTimeEmployee()
        {
            if (employeeCount >= employees.Length)
            {
                Console.WriteLine("员工数量已达上限,无法添加新员工。");
                return;
            }
            
            Console.Write("请输入员工姓名: ");
            string name = Console.ReadLine();
            
            Console.Write("请输入月薪: ");
            if (double.TryParse(Console.ReadLine(), out double salary))
            {
                FullTimeEmployee emp = new FullTimeEmployee 
                { 
                    Name = name, 
                    MonthlySalary = salary 
                };
                
                employees[employeeCount] = emp;
                employeeCount++;
                
                Console.WriteLine($"全职员工 {name} 添加成功!");
            }
            else
            {
                Console.WriteLine("输入的月薪无效,请重新操作。");
            }
        }
        
        // 添加兼职员工
        static void AddPartTimeEmployee()
        {
            if (employeeCount >= employees.Length)
            {
                Console.WriteLine("员工数量已达上限,无法添加新员工。");
                return;
            }
            
            Console.Write("请输入员工姓名: ");
            string name = Console.ReadLine();
            
            Console.Write("请输入工作小时: ");
            if (!int.TryParse(Console.ReadLine(), out int hours))
            {
                Console.WriteLine("输入的工作小时无效,请重新操作。");
                return;
            }
            
            Console.Write("请输入时薪: ");
            if (double.TryParse(Console.ReadLine(), out double rate))
            {
                PartTimeEmployee emp = new PartTimeEmployee 
                { 
                    Name = name, 
                    WorkHours = hours, 
                    HourlyRate = rate 
                };
                
                employees[employeeCount] = emp;
                employeeCount++;
                
                Console.WriteLine($"兼职员工 {name} 添加成功!");
            }
            else
            {
                Console.WriteLine("输入的时薪无效,请重新操作。");
            }
        }
        
        // 显示所有员工信息
        static void ShowAllEmployees()
        {
            if (employeeCount == 0)
            {
                Console.WriteLine("暂无员工信息。");
                return;
            }
            
            Console.WriteLine("=== 所有员工信息 ===");
            for (int i = 0; i < employeeCount; i++)
            {
                Console.Write($"{i+1}. ");
                employees[i].DisplayInfo();
            }
        }
        
        // 计算所有员工工资
        static void CalculateAllSalaries()
        {
            if (employeeCount == 0)
            {
                Console.WriteLine("暂无员工信息。");
                return;
            }
            
            Console.WriteLine("=== 员工工资计算 ===");
            double totalSalary = 0;
            
            for (int i = 0; i < employeeCount; i++)
            {
                double salary = employees[i].CalculateSalary();
                Console.WriteLine($"{employees[i].Name} 的工资: {salary}元");
                totalSalary += salary;
            }
            
            Console.WriteLine($"所有员工工资总额: {totalSalary}元");
        }
        
        // 演示多态性
        static void DemonstratePolymorphism()
        {
            Console.WriteLine("=== 多态性演示 ===");
            Console.WriteLine("创建不同类型的员工,但使用基类引用:");
            
            // 使用基类引用指向不同类型的对象
            Employee emp1 = new FullTimeEmployee { Name = "张三", MonthlySalary = 8000 };
            Employee emp2 = new PartTimeEmployee { Name = "李四", WorkHours = 100, HourlyRate = 50 };
            
            Console.WriteLine("使用相同的基类引用调用方法:");
            Console.WriteLine();
            
            Console.WriteLine("员工1:");
            emp1.DisplayInfo();
            Console.WriteLine($"计算工资: {emp1.CalculateSalary()}元");
            Console.WriteLine();
            
            Console.WriteLine("员工2:");
            emp2.DisplayInfo();
            Console.WriteLine($"计算工资: {emp2.CalculateSalary()}元");
            Console.WriteLine();
            
            Console.WriteLine("说明: 相同的调用方式(emp.DisplayInfo()和emp.CalculateSalary())");
            Console.WriteLine("但根据对象的实际类型执行了不同的实现,这就是多态性!");
        }
    }
}


评论