using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyGeneirc
{
public class Constraint
{
public static void Show<T>(T tParam)
where T : People, ISports
{
Console.WriteLine("当前类:{0},ID:{1}, Name:{2}",
typeof(Constraint), tParam.Id, tParam.Name);
tParam.Hi();
}
public static void ShowBase(People tParam)
{
Console.WriteLine("当前类:{0},ID:{1}, Name:{2}",
typeof(Constraint), tParam.Id, tParam.Name);
tParam.Hi();
}
public static T Get<T>(T t)
where T : ISports
{
t.Pinpang();
return t;
}
public static T GetClass<T>(T t)
where T : class
{
T tNew = null;
return t;
}
public static T GetStruct<T>(T t)
where T : struct
{
T tNew = default(T);
return t;
}
public static T GetNew<T>(T t)
where T : new()
{
T tNew = new T();
return t;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyGeneirc
{
internal class Program
{
static void Main(string[] args)
{
try
{
People people = new People() { Id = 123, Name = "张三" };
Chinese chinese = new Chinese() { Id = 234, Name = "李四" };
Hubei hubei = new Hubei() { Id = 456, Name = "王五" };
Japanese japanese = new Japanese() { Id = 678, Name = "田中" };
Console.WriteLine("****************约束基类***********************");
Constraint.Show<Chinese>(chinese);
Constraint.Show<Hubei>(hubei);
Console.WriteLine("****************约束接口***********************");
Constraint.Get<Chinese>(chinese);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}