IComparable<>,IFormattable,IEnumerable<>接口使用

简介: 1 Racer类 [Serializable] public class Racer : IComparable<Racer>, IFormattable { public Racer(string firstName = null, string lastName = null, string

1 Racer类

    [Serializable]
    public class Racer : IComparable<Racer>, IFormattable
    {   
                
        public Racer(string firstName = null, string lastName = null, string country = null,
            int starts = 0, int wins = 0, IEnumerable<int> years = null, IEnumerable<string> cars = null)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Country = country;
            this.Starts = starts;
            this.Wins = wins;
            var yearList = new List<int>();
            foreach (int year in years)
            {
                yearList.Add(year);
            }
            this.Years = yearList.ToArray();

            var carList = new List<string>();
            foreach (string car in cars)
            {
                carList.Add(car);
            }
            this.Cars = carList.ToArray();
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Country { get; set; }
        public int Starts { get; set; }
        public int Wins { get; set; }
        public int[] Years { get; private set; }
        public string[] Cars { get; private set; }

        public int CompareTo(Racer r)
        {
            if (r == null) throw new ArgumentNullException("r");
            return this.LastName.CompareTo(r.LastName);
        }

        public override string ToString()
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }

        public string ToString(string format)
        {
            return ToString(format, null);
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case "L":
                    return LastName;
                case "F":
                    return FirstName;
                case "C":
                    return Country;
                case "W":
                    return Wins.ToString();
                case "S":
                    return Starts.ToString();
                default:
                    throw new FormatException(string.Format("format:{0} not supported!", format));
            }
        }
    }


2 Team类

    public class Team
    {
        public Team(string name, params int[] years)
        {
            this.Name = name;
            this.Years = years;
        }

        private string Name { get; set; }
        private int[] Years { get; set; }
    }

3 初始化

            Racer racer = new Racer("Nino", "Farina", "Italy", 33, 5,
                new int[] { 1950 }, new string[] { "Alfa Romeo", "Maserati" });
            Team team1 = new Team("Vanwall", 1955);
            Team team2 = new Team("Cooper", 1959, 1960);

目录
相关文章
|
9天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。
|
人工智能 前端开发 API
前端接入通义千问(Qwen)API:5 分钟实现你的 AI 问答助手
本文介绍如何在5分钟内通过前端接入通义千问(Qwen)API,快速打造一个AI问答助手。涵盖API配置、界面设计、流式响应、历史管理、错误重试等核心功能,并提供安全与性能优化建议,助你轻松集成智能对话能力到前端应用中。
715 154
|
15天前
|
人工智能 数据可视化 Java
Spring AI Alibaba、Dify、LangGraph 与 LangChain 综合对比分析报告
本报告对比Spring AI Alibaba、Dify、LangGraph与LangChain四大AI开发框架,涵盖架构、性能、生态及适用场景。数据截至2025年10月,基于公开资料分析,实际发展可能随技术演进调整。
963 152
|
负载均衡 Java 微服务
OpenFeign:让微服务调用像本地方法一样简单
OpenFeign是Spring Cloud中声明式微服务调用组件,通过接口注解简化远程调用,支持负载均衡、服务发现、熔断降级、自定义拦截器与编解码,提升微服务间通信开发效率与系统稳定性。
366 156
|
7天前
|
分布式计算 监控 API
DMS Airflow:企业级数据工作流编排平台的专业实践
DMS Airflow 是基于 Apache Airflow 构建的企业级数据工作流编排平台,通过深度集成阿里云 DMS(Data Management Service)系统的各项能力,为数据团队提供了强大的工作流调度、监控和管理能力。本文将从 Airflow 的高级编排能力、DMS 集成的特殊能力,以及 DMS Airflow 的使用示例三个方面,全面介绍 DMS Airflow 的技术架构与实践应用。
|
8天前
|
人工智能 自然语言处理 前端开发
Qoder全栈开发实战指南:开启AI驱动的下一代编程范式
Qoder是阿里巴巴于2025年发布的AI编程平台,首创“智能代理式编程”,支持自然语言驱动的全栈开发。通过仓库级理解、多智能体协同与云端沙箱执行,实现从需求到上线的端到端自动化,大幅提升研发效率,重塑程序员角色,引领AI原生开发新范式。
588 2