0

用 C# 语言编写一个名为 Max 的泛型方法,它接受 2 个参数并返回 2 个值中的较大者。应用约束以仅支持引用类型。针对将薪水作为 int 类型属性的 Employee 类对其进行测试。Max 应根据薪水比较两个员工实例并返回具有更高薪水的员工实例。这就是我所做的我应该如何继续......?

using System;  
using System.Collections.Generic;   
using System.Text;  

namespace Generic_Max_of_Salary
{
    class Program   
    {
        static void Main(string[] args)
        {
            runtest();                
        }

        public static void runtest()    
        {
            Test_Maximum(new Employee { Salary = 10000 }, new Employee { Salary = 20000 }, new Employee { Salary = 20000 }, 1);
            Test_Maximum(new Employee { Salary = 30000 }, new Employee { Salary = 20000 }, new Employee { Salary = 30000 }, 2);
            Test_Maximum(new Employee { Salary = 10000 }, new Employee { Salary = 10000 }, new Employee { Salary = 10000 }, 3);    
        }

        public static void Test_Maximum(Employee emp1, Employee emp2, Employee obtained_answer, int testcase)
        {
            Employee expected_answer = Maximum<Employee>(emp1, emp2);

            if (CompareTo(obtained_answer, expected_answer))
            {
                Console.WriteLine("Test " + testcase + " Passed");
            }
            else
            {
                Console.WriteLine("Test " + testcase + " Failed");
            }
        }

        public static T Maximum<T>(T emp1, T emp2)
        {

            if (emp1.Salary >= emp2.Salary)
            {
                return emp1;
            }
            else
            {
                return emp2;
            }
        }

        public static bool CompareTo(Employee obtained_answer, Employee expected_answer)
        {
            if (obtained_answer.Salary == expected_answer.Salary)
            {
                return true;
            }
            else
            {
                return false;
            }
        }            
    }

    class Employee
    {
        public int Salary { get; set; }    
    }
}
4

1 回答 1

1

那是因为编译器不知道 open 类型T有一个名为Salary. 您可以添加一个约束T,它需要是一个派生类,Employee或者您需要Maximum使用薪水而不是员工实例调用该函数。

public static T Maximum<T>(T emp1, T emp2) where T: Employee
{
    if (emp1.Salary >= emp2.Salary)
    {
        return emp1;
    }
    else
    {
        return emp2;
    }
}

只是为了详细说明这个问题,一个泛型函数只限制引用类型,比如

static T Maximum<T>(T obj1, T obj2) where T : class
{
    if (obj1 > obj2)
    {
        return obj1;
    }

    return obj2;
}

不起作用,因为>未在 上定义运算符T。您最好的机会是检查输入对象是否为IComparableor Employees

static T Maximum<T>(T obj1, T obj2) where T : class
{
    if (obj1 is Employee && obj2 is Employee)
    {
        if (((Employee)obj1).Salary > ((Employee)obj2).Salary)
        {
            return obj1;
        }

        return obj2;
    }

    if (obj1 is IComparable && obj2 is IComparable)
    {
        if (((IComparable)obj1).CompareTo(obj2) > 0)
        {
            return obj1;
        }

        return obj2;
    }

    throw new NotSupportedException("Cannot compare two reference types without knowledge of the type.");
}
于 2013-08-13T07:49:51.013 回答