c# - 创建子类的实例是否创建父类的实例?

标签 c# .net

我是 C# 的新手,我想知道,如果我创建子类的实例,它是否也会自动创建父类的实例或什么?

这是我的代码:

class Program
{

    public class ParentClass
    {
        public ParentClass()
        {
            Console.WriteLine("ChildClass uses my Ctor ");
        }

    }

    public class ChildClass : ParentClass
    {
        public ChildClass()
        {
            Console.WriteLine("SaySomething");
        }
    }

    public static void Main()
    {
        ChildClass child = new ChildClass();
    }
}

最佳答案

does it also automatically create an instance of the Parent class?

不是一个单独的实例;在谈到继承时,ChildClass ParentClass 实例。

换句话说,这就像:

when creating a dog, do we also create an instance of an animal?

我们不会创造狗(分开)创造动物;狗动物实例。如果我们创造一只 Poodle , Poodle 狗,动物。

关于c# - 创建子类的实例是否创建父类的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395800/

相关文章:

.net - 如何在 Windows Workflow 中的事件之间传递数据?

c# - GetShortPathNameW 找不到指定的文件

CouchDB 的 C# 库?

c# - 如何创建和保存播放列表

c# - 使用 FtpWebRequest 重命名文件

.net - 测试终结器和 IDisposable

.net - X509Certificate2 使 IIS 崩溃

c# - DropDownListFor 默认从模型中选择的项目

c# - 将字节数组转换为 SecureString

C# 将字符串元素数组传递给 Task.Run