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);

目录
相关文章
|
2天前
|
云安全 人工智能 自然语言处理
|
7天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
659 17
|
10天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
728 57
Meta SAM3开源:让图像分割,听懂你的话
|
7天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
328 116
|
10天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
482 37
|
22天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
1天前
|
Rust 安全
掌握Rust文件读取(从零开始的IO操作指南)
本教程手把手教你用Rust读取文件,涵盖`read_to_string`一次性读取和`BufReader`逐行高效读取,适合初学者掌握安全、高效的Rust文件操作,助你轻松入门系统编程。
147 113