• c#学习笔记-继承


    根据 C# 中的继承 | Microsoft Learn

    做的笔记,具体请参考https://learn.microsoft.com/zh-cn/dotnet/csharp/fundamentals/tutorials/inheritance 

    本文只是对教程中的例子进行实践。 

    一.继承

    1.1 保护级别

    1.1.1 Compiler Error CS0122

    1. namespace Classes;
    2. //只有在基类中嵌套的派生类中,私有成员才可见
    3. public class A
    4. {
    5. private int _value = 10;
    6. public class B : A
    7. {
    8. public int GetValue()
    9. {
    10. Console.WriteLine("--------B--------");
    11. return _value;
    12. }
    13. }
    14. }
    15. public class C : A
    16. {
    17. public int GetValue()
    18. {
    19. //在这里会报错Compiler Error CS0122,
    20. // CS0122:“"A._value" 不可访问,因为它具有一定的保护级别
    21. return _value;
    22. }
    23. }

    Compiler Error CS0122 | Microsoft Learn

    此错误可以参考上面的链接 

    1.1.2 只有在基类中嵌套的派生类中,私有成员才可见。

    1. namespace Classes;
    2. //只有在基类中嵌套的派生类中,私有成员才可见
    3. public class A
    4. {
    5. private int _value = 10;
    6. public class B : A
    7. {
    8. public int GetValue()
    9. {
    10. Console.WriteLine("--------B--------");
    11. return _value;
    12. }
    13. }
    14. }
    15. public class AccessExample
    16. {
    17. public static void Main(string[] args)
    18. {
    19. //A.B 是派生自 A 的嵌套类,而 C 则派生自 A。 私有 A._value 字段在 A.B 中可见。
    20. var b = new A.B();
    21. Console.WriteLine(b.GetValue());
    22. }
    23. }
    24. // The example displays the following output:
    25. // 10

    1.2 受保护成员仅在派生类中可见。

    1. namespace Classes;
    2. //只有在基类中嵌套的派生类中,私有成员才可见
    3. public class A
    4. {
    5. protected int _value = 10;
    6. public class B : A
    7. {
    8. public int GetValue()
    9. {
    10. Console.WriteLine("--------B--------");
    11. return _value;
    12. }
    13. }
    14. }
    15. public class C : A
    16. {
    17. public int GetValue()
    18. {
    19. Console.WriteLine("--------C--------");
    20. return _value;
    21. }
    22. }
    23. public class AccessExample
    24. {
    25. public static void Main(string[] args)
    26. {
    27. //A.B 是派生自 A 的嵌套类,而 C 则派生自 A。 私有 A._value 字段在 A.B 中可见。
    28. var b = new A.B();
    29. Console.WriteLine(b.GetValue());
    30. var c = new C();
    31. Console.WriteLine(c.GetValue());
    32. }
    33. }
    34. // The example displays the following output:
    35. //--------B--------
    36. //10
    37. //--------C--------
    38. //10

    总结

    • 受保护成员仅在派生类中可见。

    • 内部成员仅在与基类同属一个程序集的派生类中可见, 在与基类属于不同程序集的派生类中不可见。

    • 公共成员在派生类中可见,并且属于派生类的公共接口。

    Public访问不受到限制
    Protected允许本类以及派生类进行访问
    Internal访问仅限于当前程序集
    Protected Internal允许本类或派生类访问,注意比Internal的范围广
    Private仅允许当前类访问,派生类不能访问

    1.2重写继承的成员

     基类成员必须标记有 virtual 关键字,才能重写继承的成员。 默认情况下,基类成员没有 virtual 标记,因此无法被重写。 标记有 abstract 关键字的基类成员要求派生类必须重写它们。继承仅适用于类和接口。

    1.3 隐式继承

    1. namespace Classes
    2. {
    3. public class SimpleClass
    4. { }
    5. }
    1. namespace Classes;
    2. using System.Reflection;
    3. public class SimpleClassExample
    4. {
    5. public static void Main()
    6. {
    7. Type t = typeof(SimpleClass);
    8. BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
    9. BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
    10. MemberInfo[] members = t.GetMembers(flags);
    11. Console.WriteLine($"Type {t.Name} has {members.Length} members: ");
    12. //Type SimpleClass has 9 members:
    13. foreach (MemberInfo member in members)
    14. {
    15. string access = "";
    16. string stat = "";
    17. var method = member as MethodBase;
    18. if (method != null)
    19. {
    20. if (method.IsPublic)
    21. access = " Public";
    22. else if (method.IsPrivate)
    23. access = " Private";
    24. else if (method.IsFamily)
    25. access = " Protected";
    26. else if (method.IsAssembly)
    27. access = " Internal";
    28. else if (method.IsFamilyOrAssembly)
    29. access = " Protected Internal ";
    30. if (method.IsStatic)
    31. stat = " Static";
    32. }
    33. string output = $"{member.Name} ({member.MemberType}): {access}{stat}, Declared by {member.DeclaringType}";
    34. Console.WriteLine(output);
    35. //公共 GetType 方法:返回表示 SimpleClass 类型的 Type 对象。
    36. //GetType(Method): Public, Declared by System.Object
    37. //受保护 MemberwiseClone 方法:创建当前对象的浅表复制。
    38. //MemberwiseClone(Method): Protected, Declared by System.Object
    39. //受保护 Finalize 方法:用于在垃圾回收器回收对象的内存之前释放非托管资源。
    40. //Finalize(Method): Protected, Declared by System.Object
    41. //公共 ToString 方法将 SimpleClass 对象转换为字符串表示形式,返回完全限定的类型名称。 在这种情况下,ToString 方法返回字符串“SimpleClass”。
    42. //ToString(Method): Public, Declared by System.Object
    43. //公共实例 Equals(Object) 方法、公共静态 Equals(Object, Object) 方法和公共静态 ReferenceEquals(Object, Object) 方法。 默认情况下,这三个方法测试的是引用相等性;也就是说,两个对象变量必须引用同一个对象,才算相等。
    44. //Equals(Method): Public, Declared by System.Object
    45. //Equals(Method): Public Static, Declared by System.Object
    46. //ReferenceEquals(Method): Public Static, Declared by System.Object
    47. //公共GetHashCode 方法:计算允许在经哈希处理的集合中使用类型实例的值。
    48. //GetHashCode(Method): Public, Declared by System.Object
    49. //.ctor(Constructor): Public, Declared by Classes.SimpleClass
    50. }
    51. }
    52. }

    从上面的例子中我们可以看到,

    .NET 类型系统中的所有类型除了可以通过单一继承进行继承之外,还可以隐式继承自 Object 或其派生的类型。 Object 的常用功能可用于任何类型。

    然后可以使用反射(便于检查类型的元数据,从而获取此类型的相关信息),获取 SimpleClass 类型的成员列表。 尽管没有在 SimpleClass 类中定义任何成员,但示例输出表明它实际上有九个成员。 这些成员的其中之一是由 C# 编译器自动为 SimpleClass 类型提供的无参数(或默认)构造函数。 剩余八个是 Object(.NET 类型系统中的所有类和接口最终隐式继承自的类型)的成员。

    1.3.1 隐式继承例子

    下面的例子,用来隐式继承的toString()方法

    1. namespace Classes
    2. {
    3. public class EmptyClass
    4. { }
    5. }
    1. namespace Classes;
    2. using System.Reflection;
    3. public class SimpleClassExample
    4. {
    5. public static void Main()
    6. {
    7. EmptyClass sc = new();
    8. Console.WriteLine(sc.ToString());
    9. //Classes.EmptyClass
    10. }
    11. }

    上面例子调用 SimpleClass 从 Object 继承而来的 SimpleClass.ToString 方法。

    下表列出了可以在 C# 中创建的各种类型及其隐式继承自的类型。 每个基类型通过继承向隐式派生的类型提供一组不同的成员。

    类型类别隐式继承自
    classObject
    structValueTypeObject
    enumEnumValueTypeObject
    delegateMulticastDelegateDelegateObject

  • 相关阅读:
    makefile通用模板
    d3dx9_42.dll丢失怎么解决?有什么方法解决d3dx9_42.dll丢失问题
    物联网开发笔记(9)- 使用Wokwi仿真MicroPython on ESP32开发板实现温度和湿度检测并使用屏幕显示
    二叉树链式结构-c语言实现
    树和二叉树
    [重庆思庄每日技术分享]-ORA-16525 dg broker不可用
    【技术积累】Python中的Pandas库【二】
    Git 分支管理规范
    洗眼镜超声波清洗机用什么水清洗、小型超声波清洗机推荐
    Tyvj p1013 找啊找啊找GF
  • 原文地址:https://blog.csdn.net/hellolianhua/article/details/127841288