003days
- 泛型
- 回调
- 接口
- 委托
泛型
using System;
//泛型
//可以将类型在类、接口、方法中进行传递
//类似于--传参
//泛型的命名:是一个标识符,遵循大驼峰命名法
//泛型在类中的使用
#region
/*
class Person<T>
{//虽然说在类中并不知道T到底是什么类型
//但是并不影响可以使用T
public T t;
}
//在一个类中,可以有不止一种泛型
class Dog<T, M, D>
{
public static M m;
public D d;
}
//泛型的特点:
//1、在一个命名空间中,可以存在两个类目相同的类,但是这两个类泛型(数量)需要不一样。
class Person { }
//泛型只可以用在当前类中,泛型不能被继承
class TaiDi : Dog<int, double, string> { }
class Program
{
public static void Main(string[] args)
{//实例化一个person对象
//如果一个类中使用到了泛型,那么在使用这个类的时候必须给泛型一个类型
Person<string> xiaomin = new Person<string>();
xiaomin.t = "xiaonm";
//使用多个泛型的类
Dog<int, double, string>.m = 3.14;
//泛型在继承中的应用
TaiDi t = new TaiDi();
}
}*/
#endregion
//泛型在方法中的使用
#region
/*
public class Person
{
public static void Show<T>() { }
//1.T只作用于当前方法中,不能作用在其他方法中
//2.虽然不知道T到底是什么类型,但是可以在方法中使用
// 2.1在方法中使用:方法体,参数,返回值
//3.方法中的泛型在调用方法的时候确定
public static void Show<T>(T t) {
//如果泛型在参数中被使用,那么调用这个方法的时候可以不给T进行赋值类型
//这个时候,T的类型由实参的类型来决定的
}
//泛型在方法中的使用:泛型作为返回值
//例如:T GetComponet<T>();
}
public class Program
{
public static void Main(string[] args)
{//调用一个有泛型的方法
Person.Show<string>();
//调用一个在参数中使用泛型的方法
Person.Show(10);
}
}*/
#endregion
//泛型在接口中的应用
#region
/*
interface Iusb<T>
{
//1.T不知道是什么应用,但是可以使用这个类型
//2.泛型不能被子接口继承
void Show(T t);
}
class Mouse : Iusb<string>
{
public void Show(string t) {
}
}*/
#endregion
回调
using System;
using System.Threading;
//回调(CallBack)
//在一个类中调用另外一个类中方法
//数据下载
//需求:当用户知道什么时候数据下载完成了,就打开刚刚下载的数据
//分析
//谁第一个知道数据下载完成了?-》下载器(DownloadTool 对象)
//下载器在哪个方法中知道数据下载完成了?-》DownloadData(srting url)
//告诉哪一个用户数据下载完成了?->当前下载的用户
//用户下载完的处理?
class Person
{
//下载数据
public void DownloadData(string url)
{ //实例化一个下载对象
DownloadTool Tool = new DownloadTool();
//先设置下数据下载完成后的处理方式
Tool.complete = DealwithData;
//让这个下载工具对象来下载数据
Tool.DownlodaData(url);
}
//处理下载完成后的数据
//result:下载到的数据
public void DealwithData(string result)
{
//打印下载到的数据
ConsoleColor reiginalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(result);
Console.ForegroundColor = reiginalColor;
}
}
//数据下载完成后的委托
//reuslt:下载到的数据
public delegate void DownloadComplete(string reuslt);
//下载工具(模拟迅雷等下载工具)
class DownloadTool
{
//不要使用传user的方式来实现需求
//因为一旦牵涉到扩展,例如再来一个新的类需要下载数据
//这里的代码需要大改
//public person user;
//让这个下载器在下载完以后,触发一个回调
public DownloadComplete complete;
//下载数据
public void DownlodaData(string url)
{
for(int i = 0; i <= 100; i += 10)
{//线程休眠:让程序休眠10毫秒
Thread.Sleep(10);
//清楚屏幕显示的内容
Console.Clear();
Console.WriteLine($"数据下载中....({i}%)"); }
Console.WriteLine("恭喜,数据下载完成");
//当数据下载完成后,直接调用当前下载完成的委托方法
complete("hello,world");
}
}
class Program
{
public static void Main(string[] args)
{//实例化一个person对象
Person xiaomin = new Person();
xiaomin.DownloadData("http:// .com");
}
}
接口
using System;
//接口
#region
/* 1.一系列的规范
2.语法:(1)如果一个类的后面既有父类也有接口,一定是父类在前
(2)规范:接口命名以大写字母I开头
(3)一个类可以实现多个接口
(4)接口中可以包含接口方法和属性访问器
(1).接口中的方法不是抽象方法
接口: 3.注意事项:(2).接口中的方法不能有访问修饰符
(3).实现接口方法的时候:1.接口中的方法权限必须是Public权限
2.实现接口方法不能使用override
3.抽象类实现接口,可以把接口方法实现为抽象方法
4.接口中的方法可以被实现为虚方法
1.接口的引用可以指向实现类的对象
多态进阶:
2.转型:(1).由实现类类型转型为接口类型 向上转型
(2).由接口类型转型为实现类类型 向下类型
*/
#endregion
//接口基本
#region
//interface
//定义一个use接口
interface IUSB
{
void Charge(); //充电
void TransportDate(); //数据传输
}
interface Isb { }
class Haedware { }
//父类在前
//一个类可以实现多个接口
//让一个类去实现接口
class Mouse : Haedware,IUSB,Isb
{
public void Charge() { }
public void TransportDate() { }
}
#endregion
//小明请保姆
#region
//家政服务规范
interface IHouseWorkService
{
void Wash();
void Cook();
void Clean();
}
interface ITakeCareSeivice
{
void Cook();
}
//人类
class Person
{
public string name;
//小明让“保姆”去洗衣服
public void LetDO (IHouseWorkService s){
s.Cook();
//向上转型
/* IHouseWorkService i = new Robot();
IHouseWorkService ii = new GirlFriend();
IHouseWorkService iii = new Nurse();
//向下转型
if (ii is GirlFriend) {
GirlFriend g = ii as GirlFriend;*/
}
}
//保姆类
class Nurse : Person, IHouseWorkService
{public void Wash()
{
Console.WriteLine(name+"在洗衣服");
}
public void Cook()
{
Console.WriteLine(name + "在做饭");
}
public void Clean()
{
Console.WriteLine(name + "在打扫卫生");
}
}
//机器人类
class Robot : IHouseWorkService
{
public void Wash()
{
Console.WriteLine( "机器人在洗衣服");
}
public void Cook()
{
Console.WriteLine("机器人在做饭");
}
public void Clean()
{
Console.WriteLine( "机器人在打扫卫生");
}
}
//女朋友类
class GirlFriend : Person, IHouseWorkService, ITakeCareSeivice
{
public void Wash() { }
public void Cook() { }
//两个接口都有这个方法,但是只要实现一次就可以了
public void Clean() { }
}
#endregion
//接口中还可以写属性访问器
#region
//实现类中需要一个属性叫Age,并提供set和get访问器
public interface IStupid
{
int Age
{
set; get;
}
}
public class XiaoMin:IStupid
{
public int Age
{
set { }
get { return 1; }
}
}
#endregion
委托
using System;
//委托:就是一个方法类型
//基本概念
#region
/*
//声明一个委托,委托的名字是 TestDelegate
delegate void TestDelegate();
delegate int TestDelegate1(int a, int b);
class Program
{
public static void Main(string[] args)
{ //实例化一个委托对象需要一个方法来实例化
//这个方法的返回值类型和参数列表需要和委托保持一致
//a其实指向一个方法TestMethod
TestDelegate a = new TestDelegate(TestMethodA);
TestDelegate1 b = new TestDelegate1(TestA);
//a,b其实是一个方法,所以可以这样使用
a();
b(3, 2);
//声明一个委托变量,并赋值
TestDelegate1 method;
//让用户从控制台输入x,y;
//需求
//如果x>y则x-y;
//如果x<y则x+y;
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
if (x > y) { method = TestB; }
else { method = TestA; }
method(x, y);
//组合委托
TestDelegate test = TestMethodA;
test += TestMethodB;//输出A和B
test();
test -= TestMethodB;//输出A
test -= TestMethodB;//还是输出A,没有的再减也没事
test();
}
public static void TestMethodA()
{
Console.WriteLine("TestMethod A Invoke");
}
public static void TestMethodB()
{
Console.WriteLine("TestMethod B Invoke");
}
public static int TestA(int a,int b)
{
return a + b;
}
public static int TestB(int a,int b)
{
return a - b;
}
}
*/
#endregion
//委托的使用
#region
/*
//LOL
//设计一个技能池
//存放所有的技能
public static class Skills
{
public static void DaBaoJian()
{
Console.WriteLine("大宝剑");
}
public static void NaoCanPi()
{
Console.WriteLine("脑残劈");
}
public static void ShenPan()
{
Console.WriteLine("审判");
}
}
public delegate void SkillDelegate();
public class Hero
{
public SkillDelegate q;
}
public class Progam
{
public static void Main(string[] args)
{//实例化一个Hero对象
Hero yasuo = new Hero();
yasuo.q = Skills.DaBaoJian;
Hero NuoShou = new Hero();
NuoShou.q= Skills.NaoCanPi;
yasuo.q();
NuoShou.q();
}
}*/
#endregion
//匿名方法和Lambda表达式
#region
/*
class Progam
{
delegate void TestDelegate();
delegate int TestDelegate2(int a,int b);
delegate int TestDelegate3(int a);
public static void Main(string[] args)
{//匿名方法:没有方法名字的方法
//可以用关键字delegate来代替方法名
TestDelegate a = delegate ()
{
Console.WriteLine("hello world");
};
TestDelegate2 b = delegate (int x, int y) { return x + y; };
//Lambda表达式
//=> lambda表达式,读作 goes to
TestDelegate a1 = () => { Console.WriteLine("hello world"); };
TestDelegate2 b1 = (int x, int y) => { return x + y; };
//简化1:如果Lambda方法体只有一个返回值,那么大括号和return可以省略;
b1 = (int x, int y) => x + y;
//简化2:在Lambda的参数列表中,参数类型可以省略
b1 = (x, y) => x + y;
//简化3:在Lambda中,如果参数列表中的参数只有一个参数,那么参数列表的小括号可省略
TestDelegate3 c = x => x * x;
}
}*/
#endregion