我正在处理一项任务,但我无法理解其中的这一部分:
Define a delegate bool GreaterOf(Comparable obj1, Comparable obj2) (obj1 is greater than obj2) to compare Comparable objects in terms of SizeOf(); For each of the structs Point, Vector and Triangle define a private method GetSizeOf(Comparable obj1, Comparable obj2) to implement the delegate GreaterOf for the respective struct. Define a property to get the instance of GreaterOf for GetSizeOf().
这里,Comparable 是一个具有该方法声明的接口(interface):
double SizeOf();
我有三个实现它的结构(Point、Vector、Triangle)。在每个结构中,我都定义了方法 GetSizeOf,如下所示:
对于点结构:
private bool GetSizeOf (Point obj1, Point obj2)
{
return obj1.SizeOf() > obj2.SizeOf();
}
我不明白的是: 为 GetSizeOf() 定义一个属性以获取 GreaterOf 的实例。
编辑: 如果这会有所帮助,请进一步了解它的情况:
Define a BubbleSort( Comparable[], GreaterOf g) method to sort an array of Comparable objects, where the delegate GreaterOf determines the ordering sequence (Assume the elements of Comparable[] are all Points, Vectors or Triangles only)
最佳答案
我猜你的导师喜欢有一个公共(public)属性,它指向你的私有(private)方法GetSizeOf
。
public interface Comparable
{
double SizeOf();
}
public delegate bool GreaterOf(Comparable obj1, Comparable obj2);
public struct Point
{
private bool GetSizeOf(Comparable obj1, Comparable obj2)
{
return obj1.SizeOf() > obj2.SizeOf();
}
private GreaterOf _pointGreaterOf;
public Point(object a)
: this()
{
var v = a; //it make no sense as is only there to prove the property assignment.
PointGreaterOf = GetSizeOf;
}
public GreaterOf PointGreaterOf
{
get { return _pointGreaterOf; }
set { _pointGreaterOf = value; }
}
}
public struct Trigangle
{
public GreaterOf TrigangleGreaterOf { get; set; }
private bool GetSizeOf(Comparable obj1, Comparable obj2)
{
return obj1.SizeOf() > obj2.SizeOf();
}
public Trigangle(object a)
: this()
{
var v = a;//it make no sense as is only there to prove the property assignment.
TrigangleGreaterOf = GetSizeOf;
}
}
public struct Vector
{
public GreaterOf VectorGreaterOf { get; set; }
private bool GetSizeOf(Comparable obj1, Comparable obj2)
{
return obj1.SizeOf() > obj2.SizeOf();
}
public Vector(object a)
: this()
{
var v = a; //it make no sense as is only there to prove the property assignment.
VectorGreaterOf = GetSizeOf;
}
}
关于c# - 如何理解这个任务需要什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20705186/