亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

理解 C# 嵌套类:定义、访问与继承实战

理解 C# 嵌套类

通过实战示例探索如何在 C# 中定义、访问和继承嵌套类,演示最佳实践。

C# 中类可声明在另一个类内部。内部类型称为嵌套类。例如:

class OuterClass {
    // 外部类成员
    class InnerClass {
        // 内部类成员
    }
}

此例在 OuterClass 内创建 InnerClass。除非访问修饰符允许,内部类型仅在外部类型内可见。


访问嵌套类成员

使用嵌套类型需分别实例化各类。

1. 创建外部类实例

OuterClass outer = new OuterClass();

outer 为 OuterClass 对象。

2. 创建嵌套类实例

OuterClass.InnerClass inner = new OuterClass.InnerClass();

因 InnerClass 嵌套在 OuterClass 内,必须用外部类型名限定。

实例化后可通过对象引用调用成员。

示例:含方法的嵌套类

using System;
namespace NestedClassDemo {
    public class Car {
        public void DisplayCar() => Console.WriteLine("Car: Bugatti");
        public class Engine {
            public void DisplayEngine() => Console.WriteLine("Engine: Petrol Engine");
        }
    }
    class Program {
        static void Main(string[] args) {
            Car sportsCar = new Car();
            sportsCar.DisplayCar();
            Car.Engine petrolEngine = new Car.Engine();
            petrolEngine.DisplayEngine();
            Console.ReadLine();
        }
    }
}

输出

Car: Bugatti
Engine: Petrol Engine

展示如何创建外部类 Car 和嵌套类 Engine 对象并调用其方法。

注意不能从外部实例直接调用嵌套类方法:

// 无效
sportsCar.DisplayEngine();

从嵌套类内部访问外部类成员

嵌套类可引用其包含类型的成员。可使用外部类型实例或外部类型名(针对静态成员)。

使用实例访问实例成员

using System;
namespace NestedClassDemo {
    public class Car {
        public string Brand = "Bugatti";
        public class Engine {
            public void ShowBrand() {
                Car car = new Car();
                Console.WriteLine("Brand: " + car.Brand);
            }
        }
    }
    class Program {
        static void Main(string[] args) {
            Car.Engine engine = new Car.Engine();
            engine.ShowBrand();
            Console.ReadLine();
        }
    }
}

输出

Brand: Bugatti

无需实例化访问静态成员

using System;
namespace NestedClassDemo {
    public class Car {
        public static string Brand = "Bugatti";
        public class Engine {
            public void ShowBrand() {
                Console.WriteLine("Brand: " + Car.Brand);
            }
        }
    }
    class Program {
        static void Main(string[] args) {
            Car.Engine engine = new Car.Engine();
            engine.ShowBrand();
            Console.ReadLine();
        }
    }
}

输出

Brand: Bugatti

涉及嵌套类的继承

嵌套类像其他类型一样参与继承。

从外部类派生

using System;
namespace NestedClassDemo {
    class Computer {
        public void Display() => Console.WriteLine("Method of Computer class");
        public class CPU {
            // 可添加额外成员
        }
    }
    class Laptop : Computer {
        // Laptop 继承 Computer 所有成员
    }
    class Program {
        static void Main(string[] args) {
            Laptop laptop = new Laptop();
            laptop.Display();
            Console.ReadLine();
        }
    }
}

输出

Method of Computer class

从嵌套类派生

using System;
namespace NestedClassDemo {
    class Computer {
        public class CPU {
            public void Display() => Console.WriteLine("Method of CPU class");
        }
    }
    class Laptop : Computer.CPU {
        // Laptop 继承 Computer.CPU 成员
    }
    class Program {
        static void Main(string[] args) {
            Laptop laptop = new Laptop();
            laptop.Display();
            Console.ReadLine();
        }
    }
}

输出

Method of CPU class

继承嵌套类时,必须用外部类名限定类型,如 Computer.CPU。

C语言

  1. C# 类与对象:面向对象编程的基石
  2. 理解 C# 访问修饰符:public、private、protected、internal 等完全指南
  3. C# 构造函数:类型、示例与高级模式完全指南
  4. C# static 关键字:静态变量、方法与类完全掌握
  5. 掌握 C# 抽象类与抽象方法:实战指南
  6. 理解 C# 嵌套类:定义、访问与继承实战
  7. 理解 C# 分部类与分部方法
  8. 掌握 C# sealed 类与方法:用法、示例与代码安全性能提升
  9. 掌握 C# 构造函数重载:灵活创建对象的多种姿势
  10. C++多态实战指南:虚函数、动态绑定与纯虚函数
  11. C++数据抽象实战:接口与实现分离的设计艺术
  12. C# 程序结构解析:从 Hello World 看命名空间、类、Main 入口与编译运行