C# 泛型 接口约束
来源:互联网 发布:多表查询的sql语句 编辑:程序博客网 时间:2024/06/05 07:52
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace interfaceConstraint{ interface IPhoneNumber { String Name { get; set; } string Number { get; set; } } class Friend : IPhoneNumber { string name; string number; public string Name { get { return name; } set { name = value; } } public string Number { get { return name; } set { name = value; } } public Friend(string name, string number) { this.name = name; this.number = number; } } class Supplier : IPhoneNumber { string name; string number; public string Name { get { return name; } set { name = value; } } public string Number { get { return name; } set { name = value; } } public Supplier(string name, string number) { this.name = name; this.number = number; } } class others { } class PhoneList<T> where T : IPhoneNumber { T[] phList; int end; public PhoneList() { phList=new T[10]; end = 0; } public bool Add(T newNumber) { if (end == 10) return false; phList[end] = newNumber; end++; return true; } public void findByName(string name) { for (int i = 0; i < end; i++) if (phList[i].Name == name) { Console.WriteLine("姓名是{0}的电话号码是{1}",name,phList[i].Number); return; } Console.WriteLine("没有找到"); } } class Program { static void Main(string[] args) { PhoneList<Friend> phList1 = new PhoneList<Friend>(); phList1.Add(new Friend("zhangsan", "123")); phList1.Add(new Friend("lisi", "234")); phList1.findByName("lisi"); Console.WriteLine("============"); PhoneList<Supplier> phList2 = new PhoneList<Supplier>(); phList1.Add(new Friend("zhangsan1", "123")); phList1.Add(new Friend("lisi1", "234")); phList1.findByName("lisi11"); Console.ReadLine(); } }}
0 0
- C# 泛型 接口约束
- C# 泛型,泛型约束,泛型接口
- C#泛型 where约束
- C# 泛型 WHERE 约束
- c#泛型 约束 和 new()约束
- C# 泛型new约束关键字
- C# 泛型 基类约束
- C# 反射-不需要接口约束,调用各种方法
- C#约束
- C# 泛型 引用类型约束 值类型约束
- C# 泛型 new()构造函数约束
- c# 泛型 类型参数的约束(C# 编程指南)
- c# 泛型 类型参数的约束(C# 编程指南)
- C#动静结合编程之一: 接口和委托的约束强度 [转]
- CLR Via C#读书笔记——接口约束【2010-01-09】
- C# 2.0 约束
- C#访问约束问题
- c#泛型约束
- 我的博客开通啦
- 205Isomorphic Strings
- hdu 1247 Hat’s Words 字典树
- acdream 1714(贪心)
- c++
- C# 泛型 接口约束
- PHP函数遍历数组
- 203Remove Linked List Elements
- acdream 1715(贪心)
- Maze, amazing
- 关于 语言 编译与解释的理解
- Linux如何编译安装源码包软件
- 设计一个点类,其中包含一对坐标点数据成员,一个计算两点距离的友元函数和坐标点的显示函数
- 如何去除table中的内部边框