泛型接口
来源:互联网 发布:如何提高淘宝网淘气值 编辑:程序博客网 时间:2024/04/29 11:14
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
{
}
}
- (枚举,结构,接口,泛型接口约束)
- 泛型接口
- 泛型接口
- 泛型接口
- IDictionary 泛型接口
- 泛型接口
- 泛型接口
- 泛型接口
- JAVA 泛型接口
- c#泛型接口
- 15.3 泛型接口
- java 泛型接口
- java 泛型接口
- Processor泛型接口
- 泛型#泛型接口#
- Java泛型接口
- 泛型接口
- 泛型接口
- objective C 基础 类的组成
- adb shell sqlite3 not found problem solved
- 如何向别人介绍你做的东西
- 多线程同步:互斥对象、事件对象、关键代码段
- 刘如佳书p162(动态规划)类似例子!
- 泛型接口
- Delphi 面向对象编程思想读书笔记
- 把电脑变成无线滤油器
- Visual studio中编译和使用libpng和zlib
- 不要用函数的指针参数去获取函数内部的内存
- pmp学习1——基础概念
- 用jacob.jar写word的一点体会
- Android AppWidget核心之AppWidgetService
- SQLite3数据库Native C++封装类(Unicode)CppSQLite3U的初步认