纲要

  • 类声明的全貌(声明既定义)
  • 最简单的类声明(类的访问控制)
  • 类成员的访问控制
  • 类的继承
    • 派生类对基类的成员获得与访问
    • 在派生类中访问基类的成员
    • 构造器的不可继承性

类声明的全貌(Class declarations)

C#有一个全局名称空间,项目中不要使用
image.png

Class Modifiers

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace MyLib.MyNamespace //文件夹的名字相当于名称空间的名字,MyLib大名称空间 MyNamespace子名称空间
  5. {
  6. public class Calculator //class关键字 Calculator类名 {}类体 前面默认为internal关键词
  7. {
  8. //internal关键词可以在一个项目中自由访问的,项目级别(Assembly)程序集
  9. public double Add(double a,double b)
  10. {
  11. return a + b;
  12. }
  13. }
  14. }

Tip.net standard2.0 要使用.net framework 4.6.1以上才能引用。

优质代码

优质的开源项目代码就是最好的参考,经常阅读高手写的代码,不但能提高自己理解代码的能力,还能渐渐养成写出漂亮代码的习惯,先临后摹。

类的继承

  • 类在功能上的扩展
  • 只能有一个基类,但可以实现多个其接口
  • 类访问级别对继承的影响(子类的访问级别不能超越父类的访问级别)
  • sealed类不能被继承(sealed封闭类,不能用来当作基类)

    继承的现象

    基类-派生类 父类(parent class)-子类(child class)
    查看是否具有继承关系: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace HelloOOP { class Program { static void Main(string[] args) { Type type = typeof(Car); Type type1 = type.BaseType; Console.WriteLine(type1.FullName); } }

  1. class Vehicle
  2. {
  3. }
  4. class Car:Vehicle
  5. {
  6. }

}

  1. <a name="pEWq5"></a>
  2. ### 是一个 is a 一个子类的实例从语义上来说也是一个父类的实例<br />
  3. ```csharp
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace HelloOOP
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. //是一个 is a 一个子类的实例从语义上来说也是一个父类的实例
  16. Car car = new Car();
  17. Console.WriteLine(car is Vehicle); // Result is true
  18. }
  19. }
  20. class Vehicle
  21. {
  22. }
  23. class Car:Vehicle
  24. {
  25. }
  26. }

可以用父类类型的变量来引用一个子类类型的实例(多态)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace HelloOOP
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //是一个 is a 一个子类的实例从语义上来说也是一个父类的实例
  13. //可以用一个父类类型的变量来引用子类类型的实例(多态)
  14. Vehicle vehicle = new Car();
  15. Object o = new Vehicle();
  16. Object o1 = new Car();
  17. }
  18. }
  19. class Vehicle
  20. {
  21. }
  22. class Car:Vehicle
  23. {
  24. }
  25. }

继承的本质

继承的本质是派生类在基类已有成员的基础上,对基类进行的横向和纵向的扩展
横向扩展:类成员在数量上越来越多
纵向扩展:不增加类成员的个数,对某个类成员的版本进行更新(时间轴和时间线) **重写(Override)**

成员的继承与访问

类成员的访问级别

类成员的私有成员被继承但是无法访问而已
**protected 只有继承的子类才能被访问,访问级别是跨程序集的,多数应用在方法上,重写用的多。internal protected组合使用,既可以被派生类访问,又可以被程序集中所有的类访问。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Vehicel //如果没有向外可访问的类,则名称空间无法被using
  7. {
  8. public class Vehicle
  9. {
  10. private int _rpm;//下划线加变量名表示私有实例变量
  11. public void Accelerate()
  12. {
  13. _rpm += 1000;
  14. }
  15. public int Speed { get { return _rpm / 100; } }
  16. }
  17. public class Car: Vehicle
  18. {
  19. }
  20. }
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Threading.Tasks;
  26. using Vehicel;
  27. namespace HelloOOP
  28. {
  29. class Program
  30. {
  31. static void Main(string[] args)
  32. {
  33. Car car = new Car();
  34. car.Accelerate();
  35. car.Accelerate();
  36. Console.WriteLine(car.Speed);
  37. }
  38. }
  39. }

构造器的不可继承性

构造器会被覆盖,基类的构造器先被触发,然后触发子类的断点。
base关键字只能向上访问一层

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace HelloOOP
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Car car = new Car();
  13. Console.WriteLine(car.Owner);
  14. }
  15. }
  16. class Vehicle
  17. {
  18. public Vehicle()
  19. {
  20. this.Owner = "N/A";
  21. }
  22. public string Owner { get; set; }
  23. }
  24. class Car:Vehicle
  25. {
  26. public Car()
  27. {
  28. this.Owner = "Car Owner";
  29. }
  30. public void ShowOwner()
  31. {
  32. Console.WriteLine(base.Owner);//base关键字只能向上访问一级
  33. Console.WriteLine(this.Owner);
  34. }
  35. }
  36. class RaceCar:Car
  37. {
  38. }
  39. }
  1. class Vehicle
  2. {
  3. public Vehicle(string owner)
  4. {
  5. this.Owner = owner;
  6. }
  7. public string Owner { get; set; }
  8. }
  9. class Car:Vehicle
  10. {
  11. public Car(string owner):base(owner) //传一个值给基类的自定义构造器
  12. {
  13. this.Owner = "Car Owner";
  14. }
  15. public void ShowOwner()
  16. {
  17. Console.WriteLine(base.Owner);//base关键字只能向上访问一级
  18. Console.WriteLine(this.Owner);
  19. }
  20. }

面向对象的实现风格

Class-based 基于类的封装继承多态

Prototype-based 基于元型的继承(JavaScript)