c#父类对象引用指向子类与方法重写

2021-07-01 01:06

阅读:411

标签:main   names   line   自动   自动调用   运行   ons   重写   ntc   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp_test
{
    class Parentclass
    {
        public Parentclass()
        {
            Console.WriteLine("this is parentclass‘s cunstructor");
        }

        public  void print()
        {
            Console.WriteLine("this is parent ‘s print");
        }
    }

    class Childclass : Parentclass
    {
        public Childclass()
        {
            Console.WriteLine("this is childclass‘s cunstructor");
        }

        public  void print()
        {
            Console.WriteLine("this is child ‘s print");
        }

        static void Main(string []args)
        {
            Parentclass p = new Parentclass();
            p.print();
            Parentclass c = new Childclass();
            c.print();
        }
    }
}

this is parentclass‘s cunstructor
this is parent ‘s print
this is parentclass‘s cunstructor
this is childclass‘s cunstructor
this is parent ‘s print
运行结果如上,可见,如果声明一个父类对象指向子类对象,会调用父类的方法。

如果将子类的print方法删除,仍然会调用父类的方法,但是如果删除父类的方法,就会报错。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp_test
{
    class Parentclass
    {
        public Parentclass()
        {
            Console.WriteLine("this is parentclass‘s cunstructor");
        }

        public  void print()
        {
            Console.WriteLine("this is parent ‘s print");
        }
    }

    class Childclass : Parentclass
    {
        public Childclass()
        {
            Console.WriteLine("this is childclass‘s cunstructor");
        }

        public  void print()
        {
            Console.WriteLine("this is child ‘s print");
        }

        static void Main(string []args)
        {
            Parentclass p = new Parentclass();
            p.print();
            Childclass c = new Childclass();
            c.print();
        }
    }
}

this is parentclass‘s cunstructor
this is parent ‘s print
this is parentclass‘s cunstructor
this is childclass‘s cunstructor
this is child ‘s print

运行结果如上,可见,如果声明子类对象指向子类对象,会调用子类的方法。

如果删除了子类方法,那么就会自动调用父类的print方法。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp_test
{
    class Parentclass
    {
        public Parentclass()
        {
            Console.WriteLine("this is parentclass‘s cunstructor");
        }

        public virtual void print()
        {
            Console.WriteLine("this is parent ‘s print");
        }
    }

    class Childclass : Parentclass
    {
        public Childclass()
        {
            Console.WriteLine("this is childclass‘s cunstructor");
        }

        public override void print()
        {
            Console.WriteLine("this is child ‘s print");
        }

        static void Main(string []args)
        {
            Parentclass p = new Parentclass();
            p.print();
            Parentclass c = new Childclass();
            c.print();
        }
    }
}

this is parentclass‘s cunstructor
this is parent ‘s print
this is parentclass‘s cunstructor
this is childclass‘s cunstructor
this is child ‘s print

结果如上所示,如果子类重写了父类的方法,那么尽管父类是指向子类的,但是仍然会调用子类的方法。

 

c#父类对象引用指向子类与方法重写

标签:main   names   line   自动   自动调用   运行   ons   重写   ntc   

原文地址:https://www.cnblogs.com/huangshizhou/p/9968269.html


评论


亲,登录后才可以留言!