using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _06泛型接口
{
class Program
{
static void Main(string[] args)
{
MyOpenClass<string> s = new MyOpenClass<string>();
Person<Human, int, Car, Car, Student> p1 = new Person<Human, int, Car, Car, Student>();
p1.Age =18;
p1.SayHi<string>("hello every, iam LDE!");
p1.SayHi<int>(121);
Console.ReadKey();
}
}
public interface ITest<T>
{
void M1(T t);
T M2();
void M3(T obj);
T PValue
{
get;
set;
}
}
//"封闭类型"
class MyClass : ITest<string>
{
public void M1(string t)
{
throw new NotImplementedException();
}
public string M2()
{
throw new NotImplementedException();
}
public void M3(string obj)
{
throw new NotImplementedException();
}
public string PValue
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
//"开发类型"
class MyOpenClass<T> : ITest<T>
{
public void M1(T t)
{
throw new NotImplementedException();
}
public T M2()
{
throw new NotImplementedException();
}
public void M3(T obj)
{
throw new NotImplementedException();
}
public T PValue
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
//可以在定义时给类型T加上约束
public class Person<T,T1,TC,TV,TK>
where T1:struct//约束T必须是值类型
where T:class//约束T1必须是引用类型
where TC:new()//约束TC必须是一个带有无参数的构造方法【要求1 要求构造函数不能是私有的,2类型不能是抽象的】
where TV: Car//约束TK类型必须是Car,或者是集成Car的子类
where TK: T
{
public T Name
{
get;
set;
}
public T1 Age
{
get;
set;
}
public void SayHi<T>(T msg)
{
Console.WriteLine(msg);
}
public TC MyCar
{
get;
set;
}
public TV otherCar
{
get;
set;
}
}
public class Car
{
public Car(string brand)
{
this.Brand = brand;
}
public Car()
{
Brand = "捷安特";
}
public string Brand
{
get;
set;
}
}
public class Human
{
}
public class Student:Human
{
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _06泛型接口
{
class Program
{
static void Main(string[] args)
{
MyOpenClass<string> s = new MyOpenClass<string>();
Person<Human, int, Car, Car, Student> p1 = new Person<Human, int, Car, Car, Student>();
p1.Age =18;
p1.SayHi<string>("hello every, iam LDE!");
p1.SayHi<int>(121);
Console.ReadKey();
}
}
public interface ITest<T>
{
void M1(T t);
T M2();
void M3(T obj);
T PValue
{
get;
set;
}
}
//"封闭类型"
class MyClass : ITest<string>
{
public void M1(string t)
{
throw new NotImplementedException();
}
public string M2()
{
throw new NotImplementedException();
}
public void M3(string obj)
{
throw new NotImplementedException();
}
public string PValue
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
//"开发类型"
class MyOpenClass<T> : ITest<T>
{
public void M1(T t)
{
throw new NotImplementedException();
}
public T M2()
{
throw new NotImplementedException();
}
public void M3(T obj)
{
throw new NotImplementedException();
}
public T PValue
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
//可以在定义时给类型T加上约束
public class Person<T,T1,TC,TV,TK>
where T1:struct//约束T必须是值类型
where T:class//约束T1必须是引用类型
where TC:new()//约束TC必须是一个带有无参数的构造方法【要求1 要求构造函数不能是私有的,2类型不能是抽象的】
where TV: Car//约束TK类型必须是Car,或者是集成Car的子类
where TK: T
{
public T Name
{
get;
set;
}
public T1 Age
{
get;
set;
}
public void SayHi<T>(T msg)
{
Console.WriteLine(msg);
}
public TC MyCar
{
get;
set;
}
public TV otherCar
{
get;
set;
}
}
public class Car
{
public Car(string brand)
{
this.Brand = brand;
}
public Car()
{
Brand = "捷安特";
}
public string Brand
{
get;
set;
}
}
public class Human
{
}
public class Student:Human
{
}
}