用IComparable和IComparableT接口实现两个类对象的比较大小.
1.IComparable接口
namespace System
{
public interface IComparable
{
//Less than zero This instance is less than obj.
//Zero This instance is equal to obj.
//Greater than zero This instance is greater than obj.
int CompareTo(object obj);
}
}
2.一个example
namespace ConsoleApplicationCompare
{
class Program
{
static void Main(string[] args)
{
Student s1 = new Student();
Student s2 = new Student();
s1.Name = "张三";
s2.Name = "张三";
s1.Score = 112;
s2.Score = 211;
Console.WriteLine(((IComparable)s1).CompareTo(s2));
Console.ReadKey();
}
}
public class Student: IComparable
{
public string Name { set; get; }
public int Score { set; get; }
public int CompareTo(object obj)
{
int result = 0;
Student o = (Student)obj;
if (this.Score > o.Score)
result = 1;
else if (this.Score == o.Score)
result = 0;
else
result = -1;
return result;
}
}
}
3.IComparable<T>接口
namespace System
{
// Type parameters: T: The type of objects to compare. That is, you can use either the type you specified or any type that is less derived.
public interface IComparable<in T>
{
int CompareTo(T other);
}
}
4. example 第二版
namespace ConsoleApplicationCompareT
{
class Program
{
static void Main(string[] args)
{
Student s1 = new Student();
Student s2 = new Student();
s1.Name = "张三";
s2.Name = "张三";
s1.Score = 112;
s2.Score = 211;
Console.WriteLine(s1.CompareTo(s2));
Console.ReadKey();
}
}
public class Student : IComparable<Student>
{
public string Name { set; get; }
public int Score { set; get; }
public int CompareTo(Student obj)
{
int result = 0;
if (this.Score > obj.Score)
result = 1;
else if (this.Score == obj.Score)
result = 0;
else
result = -1;
return result;
}
}
}
5.结论
泛型接口更好用, it's obvious.
转载于:https://www.cnblogs.com/liuzhendong/archive/2011/09/16/2178622.html
用IComparable和IComparableT接口实现两个类对象的比较大小.相关推荐
- Java写一个接口和两个类
编写一个接口和两个类,要求如下: (1)设计一个学生功能的接口,实现平均成绩的计算和基本信息的输出. (2)设计一个学生类实现了学生接口,学生类的基本信息有:学号.姓名.年龄.各科成绩(假设一共5门课 ...
- 如何判断Java中两个Class对象是否相同
如何判断两个Class对象是否相同 在JVM中表示两个class对象是否为同一个类存在两个必要条件: 类的完整类名必须一致,包括包名. 加载这个类的ClassLoader(指ClassLoader实例 ...
- C#——实现IComparableT 接口,ArrayLIst调用ArrayLIst.Sort()抛出System.InvalidOperationException异常解决方案
问题描述 未经处理的异常: System.InvalidOperationException: 未能比较数组中的两个元素. ---> System.ArgumentException: 必须至少 ...
- spring容器注入一个接口的两个实现类
转载自:https://www.cnblogs.com/linjiaxin/p/5897749.html spring容器中能拥有两个同种类型的bean吗?我有两个dao类同时实现一个接口,这两个接口 ...
- QQ空间迁移_【Frame_relay子接口的两种配置方法】
Frame relay子接口的两种配置方法 2009-05-18 17:31:39 配置Frame relay子接口 R1:R3 103:301 R2:R3 203:302 1,多点子接口R1 con ...
- Http调用第三方接口的两种方式实例《超详细!!!》***
Http调用第三方接口的两种方式<超详细!!!>* 最近在公司做一些调用第三方接口的工作,查阅了一部分的资料和向前辈以及朋友请教,完成了第三方接口的调用,其实主要是通过第三方提供的文档,完 ...
- emif接口速率问题_有关TI DSP的EMIF接口的两个问题
接口的两个问题: 地址总线不从零开始问题及寻址范围问题 作者: JBB0523 (彬彬有礼) 示例芯片: TMS320C6416 EMIF ,即 E xternal M emory I nter F ...
- Android 网络操作常用的两个类
Android SDK集成了Apache HttpClient模块.要注意的是,这里的Apache HttpClient模块是HttpClient 4.0(org.apache.http.*),而不 ...
- List list= new ArrayList为什么这样写 接口引用指向实现类的对象
经常见List<?> list= new ArrayList<?> Map<?,?> map = new HashMap<?,?> 但是发现list只 ...
最新文章
- 怎样使用Mendeley高效地管理中文文献
- Fedora GNOME 的常用快捷键
- boost::allocate_shared相关的测试程序
- Wannafly挑战赛17 - 走格子(模拟)
- [导入]Ajax使用初步
- 总结verilog产生随机数的$random和seed
- Netty入门篇-从双向通信开始
- Flink 作为现代数据仓库的统一引擎:Hive 集成生产就绪!
- 什么叫大地高_什么才叫睡得好?睡觉能满足这5个标准,说明睡眠质量高
- 又是别人家的公司!华为人才房价格曝光:1万/㎡ 带装修
- Synchronized底层实现--概论
- Python学习笔记—Dict和set
- APIcloud解决检出到指定路径:false问题
- Mac 上设置锁屏快捷键
- Intro.js 分步向导插件使用方法
- 原生JS实现图片爆炸特效
- 【专题】我国银行系科技子公司比较研究
- 在.NET中进行AutoCAD二次开发(C#+ObjectArx) (二)
- 众多交通工具3dm Rhino资源素材一键即可获取
- iOS 仿百度外卖,饿了么-商品列表页