c# - 如何理解这个任务需要什么

标签 c# interface delegates

我正在处理一项任务,但我无法理解其中的这一部分:

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/

相关文章:

ios - 实例方法 'mapView(_:didFailToLocateUserWithError:)'几乎符合可选要求

ios - 关于 ios objective-c 编程中委托(delegate)的困惑

c# - 查询 ObjectContext 中的派生实体(每个层次结构一个表)

c# - 单击通知图标时交替显示/隐藏窗口

c# - 创建用于创建新 SqlConnection 对象的工厂方法

java - 如何在java中实现接口(interface)的类中添加 protected 方法?

c# - 如何使用 Microsoft Graph 将用户添加到应用程序组和角色

Delphi插件框架

Typescript 类实现接口(interface)不尊重 readonly 修饰符

ios - 如何在@interface 行中定义两个委托(delegate)