C#——成员隐藏和重写(2021-04-29)

C#——成员隐藏和重写

成员隐藏

父类成员不用修饰符,子类隐藏成员用new修饰符修饰(可以省略)。
当子类隐藏父类成员时,若父类向下生成子类实例,则父类成员不会被隐藏,而会调用父类成员;只有当子类生成子类实例时,才会隐藏父类成员,转而使用子类成员。
上代码:

using System;
namespace test
{
    public class Humanoid
    {
        public void Yell()
        {
            Console.WriteLine("Humanoid version of the Yell() method");
        }
    }
    
    public class Enemy1 : Humanoid
    {
        public new void Yell()
        {
            Console.WriteLine("Enemy1 version of the Yell() method");
        }
    }
    
    public class Enemy2 : Humanoid
    {
        public new void Yell()
        {
            Console.WriteLine("Enemy2 version of the Yell() method");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Humanoid human = new Humanoid();
            Humanoid enemy1 = new Enemy1();
            Enemy2 enemy2 = new Enemy2();
            human.Yell();
            enemy1.Yell();
            enemy2.Yell();
            Console.ReadKey();
        }
    }
}

结果如下:

Humanoid version of the Yell() method
Humanoid version of the Yell() method
Enemy2 version of the Yell() method

重写

父类成员必须virtual修饰符,子类中要重写的成员必须override修饰符修饰。
当子类重写父类成员时,若父类向下生成子类实例,则父类成员会被重写,会调用子类成员;当子类生成子类实例时,也会重写父类成员,使用子类成员。
同样代码:

using System;

namespace test
{
    public class Humanoid
    {
        public virtual void Yell()
        {
            Console.WriteLine("Humanoid version of the Yell() method");
        }
    }
    
    public class Enemy1 : Humanoid
    {
        public override void Yell()
        {
            Console.WriteLine("Enemy1 version of the Yell() method");
        }
    }
    
    public class Enemy2 : Humanoid
    {
        public override void Yell()
        {
            Console.WriteLine("Enemy2 version of the Yell() method");
        }
    }
    
    public class Enemy3 : Humanoid
    {
        public void Yell()
        {
            Console.WriteLine("Enemy3 version of the Yell() method");
        }
    }
    
    public class Enemy4 : Humanoid
    {
        public void Yell()
        {
            Console.WriteLine("Enemy4 version of the Yell() method");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Humanoid human = new Humanoid();
            Humanoid enemy1 = new Enemy1();
            Enemy2 enemy2 = new Enemy2();
            Humanoid enemy3 = new Enemy3();
            Enemy4 enemy4 = new Enemy4();
            human.Yell();
            enemy1.Yell();
            enemy2.Yell();
            enemy3.Yell();
            enemy4.Yell();
            Console.ReadKey();
        }
    }
}

结果如下:

Humanoid version of the Yell() method
Enemy1 version of the Yell() method
Enemy2 version of the Yell() method
Humanoid version of the Yell() method
Enemy4 version of the Yell() method

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值