How to create a subclass in C#?

前端 未结 4 1374
离开以前
离开以前 2020-12-30 02:14

How do I create a subclass in C# for ASP.NET using Visual Studio 2010?

4条回答
  •  一整个雨季
    2020-12-30 02:55

    Here is an example of writing a ParentClass and then creating a ChildClass as a sub class.

    using System;
    
    public class ParentClass
    {
        public ParentClass()
        {
            Console.WriteLine("Parent Constructor.");
        }
    
        public void print()
        {
            Console.WriteLine("I'm a Parent Class.");
        }
    }
    
    public class ChildClass : ParentClass
    {
        public ChildClass()
        {
            Console.WriteLine("Child Constructor.");
        }
    
        public static void Main()
        {
            ChildClass child = new ChildClass();
    
            child.print();
        }
    }
    

    Output:

    Parent Constructor.
    Child Constructor.
    I'm a Parent Class.
    

    Rather than rewriting yet another example of .Net inheritance I have copied a decent example from the C Sharp Station website.

提交回复
热议问题