using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GenericMax
{
class Program
{
interface Order
{
Boolean greaterThan(Order other);
}
class Thing : Order
{
public int value;
public Thing(int value)
{
this.value = value; ;
}
public Boolean greaterThan(Order other)
{
// Convert the other to a thing so I can compare values
return this.value > ((Thing)other).value;
}
public override string ToString()
{
return "" + value;
}
}
static TYPE max<TYPE>(TYPE a,TYPE b) //how would I implement Order
{
return a;
}
static void Main(string[] args)
{
}
}
}
如何获取max方法中的对象以实现Order接口。 Ps:我知道max方法的逻辑是错误的。
例如在Java中,如果我想最大限度地实现Order,那就像:
<TYPE implements Order> TYPE max(TYPE a, TYPE b)
答案 0 :(得分:3)
试试这个语法:
static T max<T>(T a, T b) where T: Order
{
// Your codes.
}