You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#类方法中使用Action、Predicate委托作为入参的实现问题

问题分析

你当前编写的FindFlightWithLessPrice方法不符合要求,核心错误如下:

  • 入参设计错误:需求仅要求接收Action<Flight, double>和价格阈值两个参数,多余的Flight参数无意义
  • 委托使用逻辑错误:方法应该调用外部传入的Action实例实现自定义打印逻辑,你在方法内部重写了action变量,导致外部传入的逻辑完全不生效
  • 缺少集合遍历:没有遍历航空公司下的所有航班,仅处理了传入的单个航班实例
  • 语法错误:Lambda内部直接使用三元表达式作为独立语句不符合C#语法规则
  • 返回值设计错误:Action是无返回值的委托,作用是执行打印等副作用操作,不需要返回字符串

修正后的完整实现

1. Flight类无需修改,你现有的代码符合要求

2. 修正后的AirlineCompany类代码

using System;
using System.Collections.Generic;

namespace Assignment_4
{
    // 自定义委托,满足需求1要求
    public delegate string FlightInfoFormatter(int id);
    
    class AirlineCompany
    {
        private readonly string airlineName;
        public string AirlineName
        {
            get { return airlineName; }
        }

        private List<Flight> flights = new List<Flight>();

        public AirlineCompany(string airlineName)
        {
            this.airlineName = airlineName;
        }

        public Flight this[int index]
        {
            set
            {
                if(index >=0 && index <= flights.Count)
                    flights.Insert(index, value);
            }
            get
            {
                if(index >=0 && index < flights.Count)
                    return flights[index];
                return null;
            }
        }

        public int Count()
        {
            return flights.Count;
        }

        public string FindFlight(int flightId)
        {
            foreach (Flight f in flights)
            {
                string res = f.FindFlight(flightId);
                if (!string.IsNullOrEmpty(res))
                    return res;
            }
            return "未找到对应航班";
        }

        // 需求1实现:接收自定义委托和价格参数,价格高于给定值时调用委托
        public string ReturnFlightWithPrice(FlightInfoFormatter formatter, double priceThreshold)
        {
            string result = "";
            foreach (Flight f in flights)
            {
                if (f.Price > priceThreshold)
                {
                    result += formatter(f.Id) + Environment.NewLine + "------------------------" + Environment.NewLine;
                }
            }
            return result;
        }

        // 需求2实现:接收Action<Flight, double>,价格低于阈值时调用Action打印
        public void FindFlightWithLessPrice(Action<Flight, double> printAction, double priceThreshold)
        {
            foreach (Flight f in flights)
            {
                if (f.Price < priceThreshold)
                {
                    // 符合条件时调用传入的打印委托
                    printAction(f, priceThreshold);
                }
            }
        }

        // 需求3实现:接收Predicate,返回首个价格高于250欧元的航班
        public Flight FindFirstHighPriceFlight(Predicate<Flight> condition)
        {
            foreach (Flight f in flights)
            {
                if (condition(f))
                {
                    return f;
                }
            }
            return null;
        }
    }
}

测试用例示例

你可以用以下代码验证功能:

class Program
{
    static void Main(string[] args)
    {
        // 初始化航空公司
        AirlineCompany airline = new AirlineCompany("中国国际航空");
        // 添加测试航班
        airline[0] = new Flight(1001, "北京", "上海", "2024-06-10", 199.9);
        airline[1] = new Flight(1002, "上海", "广州", "2024-06-11", 299.5);
        airline[2] = new Flight(1003, "北京", "深圳", "2024-06-12", 159.0);
        airline[3] = new Flight(1004, "广州", "成都", "2024-06-13", 320.0);

        Console.WriteLine("=====测试需求2:打印价格低于200的航班=====");
        airline.FindFlightWithLessPrice((flight, threshold) => 
        {
            Console.WriteLine($"找到价格低于{threshold}的航班:");
            Console.WriteLine(flight.ToString());
            Console.WriteLine("------------------------");
        }, 200);

        Console.WriteLine("\n=====测试需求3:找到首个价格高于250欧元的航班=====");
        Flight highPriceFlight = airline.FindFirstHighPriceFlight(f => f.Price > 250);
        if(highPriceFlight != null)
        {
            Console.WriteLine("首个价格高于250的航班:");
            Console.WriteLine(highPriceFlight.ToString());
        }

        Console.WriteLine("\n=====测试需求1:自定义委托仅返回出发地目的地=====");
        string res = airline.ReturnFlightWithPrice(id => 
        {
            string flightInfo = airline.FindFlight(id);
            Flight f = null;
            for(int i=0;i<airline.Count();i++)
            {
                if (airline[i].Id == id) f = airline[i];
            }
            return $"航班{id}:{f.Origin} -> {f.Destination} 价格:{f.Price}";
        }, 250);
        Console.WriteLine(res);
    }
}

内容的提问来源于stack exchange,提问作者CastAway

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 00:36:03