• c# 中的类


    反射 Activator.CreateInstance

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. //反射
    6. Type t = typeof(Student);
    7. object o = Activator.CreateInstance(t, 1, "FJ");
    8. Student stu = o as Student;
    9. Console.WriteLine(stu.Name);
    10. //动态编程
    11. dynamic stu2 = Activator.CreateInstance(t, 2, "XB");
    12. Console.WriteLine(stu2.Name);
    13. }
    14. }
    15. class Student
    16. {
    17. public Student(int id, string name)
    18. {
    19. this.ID = id;
    20. this.Name = name;
    21. }
    22. public int ID { get; set; }
    23. public string Name { get; set; }
    24. }

    静态构造器只能定义静态成员

    声明一个类,创建一个实例或创建一个对象

    成员类

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. }
    6. class Student
    7. {
    8. }
    9. }

    上面的Main方法是类成员,而Student是成员类

    类的访问级别

    internal代表此类可作用于当前assembly集中。

    1. namespace MyLib.MyNamespace
    2. {
    3. internal class Calculator
    4. {
    5. public double Add(double a, double b)
    6. {
    7. return a + b;
    8. }
    9. }
    10. }
    11. namespace MyLib.MyNamespace2
    12. {
    13. class Student
    14. {
    15. public Calculator Calculator { get; set; }
    16. }
    17. }

    其中MyNamespace和MyNamespace2在同一个assembly -- MyLib中。

    类的继承

    1. namespace HelloClass
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. Type t = typeof(Car);
    8. Type tb = t.BaseType;
    9. Type tTop = tb.BaseType;
    10. Console.WriteLine(tTop.FullName);
    11. //true
    12. Console.WriteLine(tTop.BaseType==null);
    13. Vehicle vehicle = new Vehicle();
    14. //false
    15. Console.WriteLine(vehicle is Car);
    16. Car car = new Car();
    17. //true
    18. Console.WriteLine(car is Vehicle);
    19. Vehicle vehicle2 = new Car();
    20. Object o1 = new Vehicle();
    21. Object o2 = new Car();
    22. }
    23. }
    24. }
    25. class Vehicle
    26. {
    27. }
    28. class Car : Vehicle
    29. {
    30. }

    子类的实例从语义上来说也是父类的实例

    基类继承自某个基类或派生某个基类;

    某个类实现了某个基接口

    继承的本质是派生类在基类已有的成员的基础上对基类进行横向和纵向的发展

    当父类是自定义的有参构造器子类怎么解决

    1. //解决方案1
    2. public Car() : base("N/A")
    3. {
    4. this.Owner = "Car Owner";
    5. }
    6. //解决方案2
    7. public Car(string owner):base(owner)
    8. {
    9. }

    构造器是不能被继承的!!

    private表示此属性只能被类中的其他成员访问

    protected把类成员的访问级别限制在继承链上

    重写和多态

    1. class Vehicle
    2. {
    3. public virtual void Run()
    4. {
    5. Console.WriteLine("I'm running!");
    6. }
    7. }
    8. class Car:Vehicle
    9. {
    10. public override void Run()
    11. {
    12. Console.WriteLine("Car is running!");
    13. }
    14. }

    属性重写

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. Car car = new Car();
    6. car.Run();
    7. Console.WriteLine(car.Speed);
    8. Vehicle vehicle = new Vehicle();
    9. vehicle.Run();
    10. Console.WriteLine(vehicle.Speed);
    11. }
    12. }
    13. class Vehicle
    14. {
    15. private int _speed;
    16. public virtual int Speed {
    17. get {return _speed; }
    18. set {_speed=value; }
    19. }
    20. public virtual void Run()
    21. {
    22. Console.WriteLine("I'm running!");
    23. _speed = 100;
    24. }
    25. }
    26. class Car:Vehicle
    27. {
    28. private int _rpm;
    29. public override int Speed
    30. {
    31. get { return _rpm / 100; }
    32. set { _rpm = value * 100; }
    33. }
    34. public override void Run()
    35. {
    36. Console.WriteLine("Car is running!");
    37. _rpm = 5000;
    38. }
    39. }

    多态的表现内容

    多态的表现内容是当用一个父类类型的变量去引用子类类型实例的时候,当我们调用一个方法的时候,这个方法最终所被调用的版本是由对象的类型决定。它一定是能够调到这个继承链上最新的版本。最新的版本可能存在于我们的一个子类对象当中,也可能在重写过程当中被某一个隐藏给打断了

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. Vehicle car = new Car();
    6. car.Run();
    7. Console.WriteLine(car.Speed);
    8. Car vehicle = new RaceCar();
    9. vehicle.Run();
    10. }
    11. }
    12. class Vehicle
    13. {
    14. public virtual void Run()
    15. {
    16. Console.WriteLine("I'm running!");
    17. }
    18. }
    19. class Car:Vehicle
    20. {
    21. public override void Run()
    22. {
    23. Console.WriteLine("Car is running!");
    24. }
    25. }
    26. class RaceCar:Car
    27. {
    28. public override void Run()
    29. {
    30. Console.WriteLine("Race car is running !");
    31. }
    32. }

    接口和抽象类

    solid设计模式

    是五个面向对象基本设计原则的首字母的缩写,分别是:

    single responsibility principle 单一职责原则(SRP)

    open close principle 开闭原则(OCP)

    liskov substitution principle 里氏替换原则(LSP)

    interface segregation principle 接口隔离原则(ISP)

    dependency inversion principle 依赖反转原则(DIP)

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. Vehicle v = new RaceCar();
    6. v.Run();
    7. }
    8. }
    9. abstract class Vehicle
    10. {
    11. public void Stop()
    12. {
    13. Console.WriteLine("Stopped!");
    14. }
    15. public void Fill()
    16. {
    17. Console.WriteLine("Pay and fill...");
    18. }
    19. public abstract void Run();
    20. }
    21. class Car : Vehicle
    22. {
    23. public override void Run()
    24. {
    25. Console.WriteLine("Car is running!");
    26. }
    27. }
    28. class Truck : Vehicle
    29. {
    30. public override void Run()
    31. {
    32. Console.WriteLine("Truck is running!");
    33. }
    34. }
    35. class RaceCar : Vehicle
    36. {
    37. public override void Run()
    38. {
    39. Console.WriteLine("Race car is running!");
    40. }
    41. }

    接口:类中的所有成员方法都是纯虚函数抽象的,且都是public

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. Vehicle v = new RaceCar();
    6. v.Run();
    7. }
    8. }
    9. interface IVehicle
    10. {
    11. void Stop();
    12. void Fill();
    13. void Run();
    14. }
    15. abstract class Vehicle:IVehicle
    16. {
    17. public void Stop()
    18. {
    19. Console.WriteLine("Stopped!");
    20. }
    21. public void Fill()
    22. {
    23. Console.WriteLine("Pay and fill...");
    24. }
    25. public abstract void Run();
    26. }
    27. class Car : Vehicle
    28. {
    29. public override void Run()
    30. {
    31. Console.WriteLine("Car is running!");
    32. }
    33. }
    34. class Truck : Vehicle
    35. {
    36. public override void Run()
    37. {
    38. Console.WriteLine("Truck is running!");
    39. }
    40. }
    41. class RaceCar : Vehicle
    42. {
    43. public override void Run()
    44. {
    45. Console.WriteLine("Race car is running!");
    46. }
    47. }

  • 相关阅读:
    代数与逻辑:作业二 主成分分析法
    Spring底层
    论文学习——FALL-E:GAUDIO FOLEY SYNTHESIS SYSTEM
    树的基本概念及二叉树
    从0开始学习JavaScript--JavaScript 数字与日期
    nginx使用lua通过request_body按条件开放访问权限
    Unity 自制转表插件,高效便捷,无反射代码
    LeetCode 面试题 16.01. 交换数字
    8Base集团通过SmokeLoader部署新的Phobos勒索软件变种
    基于SSM的运动会管理系统
  • 原文地址:https://blog.csdn.net/ThePaK/article/details/133431795