泛型接口

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
{



}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值