使用适配器模式实现两个类对象进行比较大小
2016-11-13 12:24
357 查看
1.使用IComparer接口来作为第三方的比较方法
using System;
using System.Collections;
class Employ
{
public int Age;
public string Name;
public Employ(int age, string name)
{
Age = age;
Name = name;
}
}
class compa : IComparer
{
public int Compare(object x, object y)
{
if (x.GetType() != typeof (Employ) || y.GetType() != typeof (Employ))
return -1;
Employ e1 = (Employ) x;
Employ e2 = (Employ) y;
if (e1.Age > e2.Age)
return 1;
else
{
return 0;
}
}
}
public class Test
{
public static void Main()
{
Employ[] E=new Employ[2];
E[0] = new Employ(10,"jia");
E[1] = new Employ(15, "jia1111");
Array.Sort(E, new compa());
for(int i=0;i<2;i++)
Console.WriteLine(((Employ)E[i]).Age);
}
}
2.使用IComparable接口来实现自身对象就可以和其它对象比较
using UnityEngine;
using System.Collections;
using System;
public class Node : IComparable {
public float nodeTotalCost;
public float estimatedCost;
// a = b + a;
// estimatedCost = nodeTotalCost + estimatedCost;
// C = S + H
public bool bObstacle;
public Node parent;
public Vector3 position;
public Node()
{
this.estimatedCost = 0.0f;
this.nodeTotalCost = 1.0f;
this.bObstacle = false;
this.parent = null;
}
public Node(Vector3 pos)
{
this.estimatedCost = 0.0f;
this.nodeTotalCost = 1.0f;
this.bObstacle = false;
this.parent = null;
this.position = pos;
}
public void MarkAsObstacle()
{
this.bObstacle = true;
}
public int CompareTo(object obj)
{
Node node = (Node)obj;
if (this.estimatedCost < node.estimatedCost)
{
return -1;
}
if (this.estimatedCost > node.estimatedCost)
{
return 1;
}
return 0;
}
}
using System;
using System.Collections;
class Employ
{
public int Age;
public string Name;
public Employ(int age, string name)
{
Age = age;
Name = name;
}
}
class compa : IComparer
{
public int Compare(object x, object y)
{
if (x.GetType() != typeof (Employ) || y.GetType() != typeof (Employ))
return -1;
Employ e1 = (Employ) x;
Employ e2 = (Employ) y;
if (e1.Age > e2.Age)
return 1;
else
{
return 0;
}
}
}
public class Test
{
public static void Main()
{
Employ[] E=new Employ[2];
E[0] = new Employ(10,"jia");
E[1] = new Employ(15, "jia1111");
Array.Sort(E, new compa());
for(int i=0;i<2;i++)
Console.WriteLine(((Employ)E[i]).Age);
}
}
2.使用IComparable接口来实现自身对象就可以和其它对象比较
using UnityEngine;
using System.Collections;
using System;
public class Node : IComparable {
public float nodeTotalCost;
public float estimatedCost;
// a = b + a;
// estimatedCost = nodeTotalCost + estimatedCost;
// C = S + H
public bool bObstacle;
public Node parent;
public Vector3 position;
public Node()
{
this.estimatedCost = 0.0f;
this.nodeTotalCost = 1.0f;
this.bObstacle = false;
this.parent = null;
}
public Node(Vector3 pos)
{
this.estimatedCost = 0.0f;
this.nodeTotalCost = 1.0f;
this.bObstacle = false;
this.parent = null;
this.position = pos;
}
public void MarkAsObstacle()
{
this.bObstacle = true;
}
public int CompareTo(object obj)
{
Node node = (Node)obj;
if (this.estimatedCost < node.estimatedCost)
{
return -1;
}
if (this.estimatedCost > node.estimatedCost)
{
return 1;
}
return 0;
}
}
相关文章推荐
- 用IComparable和IComparable<T>接口实现两个类对象的比较大小.
- java使用compareTo实现一个类的对象之间比较大小
- 实现比较字符串大小的方法(不使用系统方法)
- 使用Java的动态代理技术实现对象适配器模式
- 五星-原型模式常使用于以下场景--而JAVA中的任何类只要实现了Cloneable标识接口,就可以使用clone方法来进行对象的拷贝
- 【Java】【TIJ】使用static实现对创建的类对象进行计数
- [原创]Java使用反射及自定义注解实现对象差异性比较
- 每日一题:编写一个函数,不使用算术运算符,实现比较两个数的大小
- Integer类型数据比较大小问题:(Integer定义的是对象,养成使用equals方法的好习惯)
- 面向对象,类的组合关系,继承,实现,方法重写,方法重载,this的使用,抽象方法和抽象类的比较,父类构造方法存在的意义,多态的是用和解析,各种访问修饰符
- SPCAMLEditor使用系列(3)--利用SPCamlEditor实现 时间字段跟当前时间进行实时比较计算
- 使用Ado.Net进行简单事务处理的四种实现及比较
- 一步一步教你使用AgileEAS.NET基础类库进行应用开发-WinForm应用篇-演示ORM对象与DataGridView的绑定技术-商品字典的另一个实现
- cocos2dx中Size创建的对象,比较一个Size大小使用equals
- C#基础之数组排序、对象大小比较实现代码
- 线性查找法————使用查找键逐个与数组元素进行比较实现查找
- 黑马程序员——使用equals实现对象的比较
- 对于集合中的自定义对象使用collections.sort 进行排序,需要实现compartor接口
- 报错:对象必须实现 IConvertible;以分隔符进行分割链接concat_ws的使用方法;mysql数据类型转换cast,convert
- java使用serializable进行序列化与反序列化实现对象clone(克隆)