现代编程三大技能:算法、数据结构,面向对象

泛型

Class ClassName<类型参数>
类型参数TParaName
泛型使用之前需要进行特化。

泛型类(泛型和面向对象的关系)

泛型特化之后是强类型的,能够看到Color的属性

  1. using System;
  2. namespace Generic
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Apple apple = new Apple() { Color = "Red" };
  9. Book book = new Book() { Name = "WPF" };
  10. Box<Apple> applebox = new Box<Apple>() { Cargo = apple };//泛型特化之后是强类型的,能够看到Color的属性
  11. Console.WriteLine(applebox.Cargo.Color);
  12. }
  13. }
  14. class Apple
  15. {
  16. public string Color { get; set; }
  17. }
  18. class Book
  19. {
  20. public string Name { get; set; }
  21. }
  22. class Box<TCargo>
  23. {
  24. public TCargo Cargo { get; set; }
  25. }
  26. }

泛型接口(泛型和面向对象,数据结构的关系)

两种方式:

  • 在new实例的时候特化
  • 在实现接口的时候特化

泛型接口集中名称空间:System.Collections.Generic

Generic Example1

  1. using System;
  2. namespace GenericInterface
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Student<int> student = new Student<int>();
  9. student.ID = 101; //TID被特化成了int类型
  10. student.Name = "Timonthy";
  11. }
  12. }
  13. interface IUnique<TID> //确保唯一性,但是不知道唯一性的类型是什么
  14. {
  15. TID ID { get; set; }
  16. }
  17. class Student<TID> : IUnique<TID> //一个类实现泛型接口本身也需要是泛型类
  18. {
  19. public TID ID { get; set; } //实现泛型接口的属性
  20. public string Name { get; set; }
  21. }
  22. }

Generic Example2

  1. using System;
  2. using System.Collections.Generic;
  3. namespace GenericExample1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. IList<int> list = new List<int>();
  10. for (int i = 0; i < 100; i++)
  11. {
  12. list.Add(i);
  13. }
  14. foreach (var item in list)
  15. {
  16. Console.WriteLine(item);
  17. }
  18. }
  19. }
  20. }

Generic Example3:带多个泛型参数的接口

  1. using System;
  2. using System.Collections.Generic;
  3. namespace GenericExample1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. IDictionary<int, string> dict = new Dictionary<int, string>();//多态,特化后接口类型的变量引用一个特化后接口类型的实例
  10. dict[1] = "Timonthy";
  11. dict[2] = "Michael";
  12. Console.WriteLine($"Student #1 is {dict[1]},Student #2 is {dict[2]}");
  13. }
  14. }
  15. }

泛型方法(泛型和算法的关系)

方法重载Dug有风险,泛型登场
修饰符 T 方法名(){} T类型 尖括号里面的是泛型参数

声明泛型方法:

  • 泛型方法有两个参数列表
    • 封闭在圆括号内的方法参数列表
    • 封闭在尖括号内的类型参数列表
  • 要声明泛型方法,需要
    • 在方法名称之后和方法参数列表之前放置类型参数列表;
    • 在方法参数列表后放置可选的约束子句 ```csharp Example1: static int[] Zip(int[] a,int[]b){} staitc T[] Zip(T[]a,T[]b){}

Example2: public T PrintData(S s,T t) where S:Person{return s;}

  1. <a name="ce36I"></a>
  2. ## 泛型委托
  3. Action Func<br />delegate R Mydelegate<T,R>(T value);<br />无返回值:
  4. ```csharp
  5. using System;
  6. namespace GenericDelegate
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Action<string> action =Say;
  13. action.Invoke("Timonthy");
  14. Action<int> action1 = Mul;
  15. action1.Invoke(1);
  16. }
  17. static void Say(string str)
  18. {
  19. Console.WriteLine($"Hello,{str}");
  20. }
  21. static void Mul(int x)
  22. {
  23. Console.WriteLine(x*100);
  24. }
  25. }
  26. }

有返回值

using System;

namespace GenericDelegate
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int, int> func = Add;
            var result = func.Invoke(1, 2);
            Console.WriteLine(result);

        }

       static int Add(int a,int b) {
            return a + b;
       }

        static double Add(double a,double b) {
            return a + b;
        }
    }
}

Lamda表达式

对于逻辑非常简单的方法不想去声明它,随时调用随时声明,匿名声明。
(a,b)带入到=>后面去实现 a,b的类型在Func中已经声明了

using System;
namespace Lamada {
    class Program {
        static void Main(string[] args) {
            Func<int, int, int> func = (a, b) => { return a + b; };
            var result = func.Invoke(1,2);
            Console.WriteLine($"{result}");
        }
    }
}

partial类:分部类

partial类的好处

  • 减少类的派生
  • partial类与Entity Framework 实体框架
  • partial类与Windows Form,WPF,ASP.NET Core

    实例

    Example1:ADO .NET Entity Model

    保证名称空间和类名相同
    .NETFramework可以看见,.NETCore只支持第一代
    刷新的话下面这个类会被重新生成,如果想在里面实现方法,则需要用分部类实现。
    image.png

    namespace TestDataBase {
      class Program {
          static void Main(string[] args) {
              var dbContext = new BookstoreEntities();
              var books = dbContext.Book;
              foreach (var item in books) {
                  Console.WriteLine(item.Name);
              }
          }
      }
    }
    

    分部类实现: ```csharp namespace TestDataBase { using System; using System.Collections.Generic;

    public partial class Book {

      public string Report() {
          return $"#{this.ID} Name:{this.Name} Price:{this.Price}";
      }
    

    } }

主程序:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDataBase {
    class Program {
        static void Main(string[] args) {
            var dbContext = new BookstoreEntities();
            var books = dbContext.Book;
            foreach (var item in books) {
                Console.WriteLine(item.Report());
            }          
        }
    }
}

Example:partial类允许不同的部分使用不同的编程语言

Winform:partial类都是用C#写的
WPF:C# XAML
ASP .NET Core

枚举

  • 人为限定取值范围的整数
  • 整数值的对应
  • 比特位式用法 读写文件等 ```csharp using System;

namespace Manager { class Program { static void Main(string[] args) { Person person = new Person(); person.Level = Level.Employee; person.Skill = Skill.Drive|Skill.Cook|Skill.Program|Skill.Teach; //或上,值为15 Console.WriteLine((person.Skill&Skill.Drive)>0);//与判断是否包含

        Person boss = new Person();
        boss.Level = Level.BigBoss;
        Console.WriteLine((int)Level.Manager);
    }
}

enum Level {
    Employee=100,
    Manager,
    Boss,
    BigBoss,
}

enum Skill {
    Drive=1,
    Cook=2,
    Program=4,
    Teach=8,
}

class Person {
    public int ID { get; set; }
    public string Name { get; set; }
    public Level Level { get; set; }
    public Skill Skill { get; set; }

}

}

```

结构体

  • 值类型,可装/拆箱 Object o = stu (从栈中拷贝实例到堆中)
  • 可实现接口,不能派生自类/结构体
  • 不能有显示无参数构造器