windows C#-泛型接口

为泛型集合类或表示集合中的项的泛型类定义接口通常很有用处。 为避免对值类型执行装箱和取消装箱操作,最好对泛型类使用泛型接口,例如 IComparable<T>。 .NET 类库定义多个泛型接口,以便用于 System.Collections.Generic 命名空间中的集合类。 

接口被指定为类型参数上的约束时,仅可使用实现接口的类型。 如下代码示例演示一个派生自 GenericList<T> 类的 SortedList<T> 类。SortedList<T> 添加约束 where T : IComparable<T>。 此约束可使 SortedList<T> 中的 BubbleSort 方法在列表元素上使用泛型 CompareTo 方法。 在此示例中,列表元素是一个实现 IComparable<Person> 的简单类 Person。

//Type parameter T in angle brackets.
public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
{protected Node head;protected Node current = null;// Nested class is also generic on Tprotected class Node{public Node next;private T data;  //T as private member datatypepublic Node(T t)  //T used in non-generic constructor{next = null;data = t;}public Node Next{get { return next; }set { next = value; }}public T Data  //T as return type of property{get { return data; }set { data = value; }}}public GenericList()  //constructor{head = null;}public void AddHead(T t)  //T as method parameter type{Node n = new Node(t);n.Next = head;head = n;}// Implementation of the iteratorpublic System.Collections.Generic.IEnumerator<T> GetEnumerator(){Node current = head;while (current != null){yield return current.Data;current = current.Next;}}// IEnumerable<T> inherits from IEnumerable, therefore this class// must implement both the generic and non-generic versions of// GetEnumerator. In most cases, the non-generic method can// simply call the generic method.System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator(){return GetEnumerator();}
}public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
{// A simple, unoptimized sort algorithm that// orders list elements from lowest to highest:public void BubbleSort(){if (null == head || null == head.Next){return;}bool swapped;do{Node previous = null;Node current = head;swapped = false;while (current.next != null){//  Because we need to call this method, the SortedList//  class is constrained on IComparable<T>if (current.Data.CompareTo(current.next.Data) > 0){Node tmp = current.next;current.next = current.next.next;tmp.next = current;if (previous == null){head = tmp;}else{previous.next = tmp;}previous = tmp;swapped = true;}else{previous = current;current = current.next;}}} while (swapped);}
}// A simple class that implements IComparable<T> using itself as the
// type argument. This is a common design pattern in objects that
// are stored in generic lists.
public class Person : System.IComparable<Person>
{string name;int age;public Person(string s, int i){name = s;age = i;}// This will cause list elements to be sorted on age values.public int CompareTo(Person p){return age - p.age;}public override string ToString(){return name + ":" + age;}// Must implement Equals.public bool Equals(Person p){return (this.age == p.age);}
}public class Program
{public static void Main(){//Declare and instantiate a new generic SortedList class.//Person is the type argument.SortedList<Person> list = new SortedList<Person>();//Create name and age values to initialize Person objects.string[] names =["Franscoise","Bill","Li","Sandra","Gunnar","Alok","Hiroyuki","Maria","Alessandro","Raul"];int[] ages = [45, 19, 28, 23, 18, 9, 108, 72, 30, 35];//Populate the list.for (int x = 0; x < 10; x++){list.AddHead(new Person(names[x], ages[x]));}//Print out unsorted list.foreach (Person p in list){System.Console.WriteLine(p.ToString());}System.Console.WriteLine("Done with unsorted list");//Sort the list.list.BubbleSort();//Print out sorted list.foreach (Person p in list){System.Console.WriteLine(p.ToString());}System.Console.WriteLine("Done with sorted list");}
}

可将多个接口指定为单个类型上的约束,如下所示:

class Stack<T> where T : System.IComparable<T>, IEnumerable<T>
{
}

一个接口可定义多个类型参数,如下所示:

interface IDictionary<K, V>
{
}

适用于类的继承规则也适用于接口:

interface IMonth<T> { }interface IJanuary : IMonth<int> { }  //No error
interface IFebruary<T> : IMonth<int> { }  //No error
interface IMarch<T> : IMonth<T> { }    //No error//interface IApril<T>  : IMonth<T, U> {}  //Error

如果泛型接口是协变的(即,仅使用自身的类型参数作为返回值),那么这些接口可继承自非泛型接口。 在 .NET 类库中,IEnumerable<T> 继承自 IEnumerable,因为 IEnumerable<T> 在 GetEnumerator 的返回值和 Current 属性 Getter 中仅使用 T。

具体类可实现封闭式构造接口,如下所示:

interface IBaseInterface<T> { }class SampleClass : IBaseInterface<string> { }

只要类形参列表提供接口所需的所有实参,泛型类即可实现泛型接口或封闭式构造接口,如下所示:

interface IBaseInterface1<T> { }
interface IBaseInterface2<T, U> { }class SampleClass1<T> : IBaseInterface1<T> { }          //No error
class SampleClass2<T> : IBaseInterface2<T, string> { }  //No error

控制方法重载的规则对泛型类、泛型结构或泛型接口内的方法一样。

从 C# 11 开始,接口可以声明 static abstract 或 static virtual 成员。 声明任一 static abstract 或 static virtual 成员的接口几乎始终是泛型接口。 编译器必须在编译时解析对 static virtual 和 static abstract 方法的调用。 接口中声明的 static virtual 和 static abstract 方法没有类似于类中声明的 virtual 或 abstract 方法的运行时调度机制。 相反,编译器使用编译时可用的类型信息。 这些成员通常是在泛型接口中声明的。 此外,声明 static virtual 或 static abstract 方法的大多数接口都声明了其中一个类型参数必须实现已声明的接口。 然后,编译器使用提供的类型参数来解析声明成员的类型。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/67725.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ios脚本巨魔商店多巴胺越狱基本操作教程

准备工作 确认设备兼容性&#xff1a;A9-A11&#xff08;iPhone6s&#xff0d;X&#xff09;&#xff1a;iOS15.0-16.6.1&#xff1b;A12-A14&#xff08;iPhoneXR&#xff0d;12PM&#xff09;&#xff1a;iOS15.0-16.5.1&#xff1b;A15-A16&#xff08;iPhone13&#xff0d…

一.MySQL程序简介

整体介绍 1.服务端mysqld(可执行文件) mysqld --verbose --help 2.客户端mysql(可执行文件) 3.其它工具包程序

算法练习03

一、题目 给你两个字符串 haystack和 needle&#xff0c;请你在haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从0开始)。如果 needle不是 haystack 的一部分&#xff0c;则返回-1。 示例 1:输入:haystack"sadbutsad",needle "sad"。输出…

G1垃圾回收器的FullGC

如何确定GarbageFirst回收器发生的是FullGC ? 必须出现FullGC字样才算是FUllGC&#xff0c;例如下图&#xff1a;因为内存分配失败&#xff08;Allocation Failure&#xff09;导致 如果不出现FullGC的字样说明它不是FUllGC&#xff0c;并不像Serial GC、ParallelGC的在老年代…

Hadoop常见面试题

题目摘录于博客https://blog.csdn.net/qq_42397330/article/details/130218083 1. HDFS的架构 HDFS采用主从架构&#xff0c;其中有两个重要节点Name Node和Data Node&#xff0c;前者负责管理节点以及命名空间和客户端的请求&#xff0c;后者是实际存储数据的节点&#xff0c;…

Sql 创建用户

Sql server 创建用户 Sql server 创建用户SQL MI 创建用户修改其他用户密码 Sql server 创建用户 在对应的数据库执行&#xff0c;该用户得到该库的所有权限 test.database.chinacloudapi.cn DB–01 DB–02 创建服务器登录用户 CREATE LOGIN test WITH PASSWORD zDgXI7rsafkak…

【Duilib】 List控件支持多选和获取选择的多条数据

问题 使用Duilib库写的一个UI页面用到了List控件&#xff0c;功能变动想支持选择多行数据。 分析 1、List控件本身支持使用SetMultiSelect接口设置是否多选&#xff1a; void SetMultiSelect(bool bMultiSel);2、List控件本身支持使用GetNextSelItem接口获取选中的下一个索引…

腾讯云AI代码助手编程挑战赛-武器大师

作品简介 对话过程能够介绍二战 各种武器 冷战 武器 现代的 各种武器装备&#xff0c;陆海空三军都知道。 技术架构 使用全后端分离的架构&#xff0c;前端使用Vue脚手架&#xff0c;腾讯云修改样式css 开发环境、开发流程 系统&#xff1a;win11 开发工具&#xff1a;VS…

Maven核心插件之maven-resources-plugin

前言 Maven 插件是 Maven 构建系统的重要组成部分&#xff0c;它们为 Maven 提供了丰富的功能和扩展能力&#xff0c;使得 Maven 不仅是一个构建工具&#xff0c;更是一个强大的项目管理平台。在 Maven 项目中&#xff0c;插件的使用通常通过配置 pom.xml 文件来完成。每个插件…

Golang的文件加密技术研究与应用

Golang的文件加密技术研究与应用 一、加密技术概述 文件加密的重要性 文件加密是指通过对文件进行加密操作&#xff0c;将文件内容转化为一段难以理解的数据。这样可以保护文件的隐私和安全&#xff0c;防止文件被未授权的用户访问和窃取。在日常工作和生活中&#xff0c;我们经…

3. ML机器学习

1.人工智能与机器学习的关系 机器学习是人工智能的一个重要分支&#xff0c;是人工智能的一个子集。它无需显式编程&#xff0c;而是通过数据和算法使机器能够自动学习和改进&#xff0c;从而实现智能行为。机器学习依赖于算法来识别数据中的模式&#xff0c;并通过这些模式做出…

Redis之秒杀活动

目录 全局唯一ID&#xff1a; 为什么 count 不可能为 null&#xff1f; 为什么返回值是 timestamp << COUNT_BITS | count&#xff1f; 整体的逻辑 (1) 生成时间戳 (2) 生成序列号 (3) 拼接时间戳和序列号 超卖问题&#xff1a; 基于版本号的乐观锁 CAS思想 …

VSCode 在Windows下开发时使用Cmake Tools时输出Log乱码以及CPP文件乱码的终极解决方案

在Windows11上使用VSCode开发C程序的时候&#xff0c;由于使用到了Cmake Tools插件&#xff0c;在编译运行的时候&#xff0c;会出现输出日志乱码的情况&#xff0c;那么如何解决呢&#xff1f; 这里提供了解决方案&#xff1a; 当Settings里的Cmake: Output Log Encoding里设…

如何解决 /proc/sys/net/bridge/bridge-nf-call-iptables 文件缺失的问题

在使用 Linux 系统&#xff0c;尤其是在容器化环境&#xff08;如 Docker、Kubernetes&#xff09;中时&#xff0c;我们可能会遇到如下错误信息&#xff1a; [ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptabl…

【C++经典例题】求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句

&#x1f493; 博客主页&#xff1a;倔强的石头的CSDN主页 &#x1f4dd;Gitee主页&#xff1a;倔强的石头的gitee主页 ⏩ 文章专栏&#xff1a; 期待您的关注 题目描述&#xff1a; 原题链接&#xff1a; 求123...n_牛客题霸_牛客网 (nowcoder.com) 解题思路&#xff1a; …

《软硬协同优化,解锁鸿蒙系统AI应用性能新高度》

在当今数字化时代&#xff0c;鸿蒙系统与人工智能的融合正逐渐成为科技领域的热门话题。如何通过软件和硬件协同优化&#xff0c;进一步提升鸿蒙系统中AI应用的整体性能&#xff0c;成为了开发者和技术爱好者们关注的焦点。 鸿蒙系统与AI应用的融合现状 鸿蒙系统以其独特的微…

STM32 单片机 练习项目 LED灯闪烁LED流水灯蜂鸣器 未完待续

个人学习笔记 文件路径&#xff1a;程序源码\STM32Project-DAP&DAPmini\1-1 接线图 3-1LED闪烁图片 新建项目 新建项目文件 选择F103C8芯片 关闭弹出窗口 拷贝资料 在项目内新建3个文件夹 Start、Library、User Start文件拷贝 从资料中拷贝文件 文件路径&#xff1a;固…

DAY15 神经网络的参数和变量

DAY15 神经网络的参数和变量 一、参数和变量 在神经网络中&#xff0c;参数和变量是两个关键概念&#xff0c;它们分别指代不同类型的数据和设置。 参数&#xff08;Parameters&#xff09; 定义&#xff1a;参数是指在训练过程中学习到的模型内部变量&#xff0c;这些变量…

VS Code 可视化查看 C 调用链插件 C Relation

简介 一直想用 SourceInsight 一样的可以查看函数调用链一样的功能&#xff0c;但是又不想用 SourceInsight&#xff0c;找了一圈没有找到 VS Code 有类似功能的插件&#xff0c;索性自己开发了一个。 这是一个可以可视化显示 C 函数调用关系的 VS Code 插件&#xff0c;功能纯…

C#语言的字符串处理

C#语言的字符串处理 引言 在现代编程中&#xff0c;字符串处理是一项重要的技能&#xff0c;几乎在所有编程语言中都有应用。C#语言作为一种强类型的、面向对象的编程语言&#xff0c;提供了丰富的字符串处理功能。这使得开发人员能够方便地进行文本操作&#xff0c;比如字符…