隐藏基类中存在的方法

本文关键字:方法 存在 基类 隐藏 | 更新日期: 2023-09-27 18:02:07

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LearnOverride
{
    class Program
    {
        static void Main(string[] args)
        {
            Owner owner = new Owner();
            Safe safe = new Safe();
            Console.WriteLine("When 'tLocksmith locksmith = new Locksmith();'n");
            Locksmith locksmith = new Locksmith();
            locksmith.OpenSafe(safe, owner);
            Console.WriteLine("when ReturnContents() called from main,");
            Jewels openedLocksmith = safe.Open("12345");
            locksmith.ReturnContents(openedLocksmith, owner);
            Console.WriteLine();
            Console.WriteLine("'n'nWhen 'tJewelThief jewelThief = new JewelThief();'n");
            JewelThief jewelThief = new JewelThief();
            jewelThief.OpenSafe(safe, owner);
            Console.WriteLine("when ReturnContents() called from main,");
            Jewels opened = safe.Open("12345");
            jewelThief.ReturnContents(opened, owner);
            Console.WriteLine();
            Console.WriteLine("'n'nWhen 'tLocksmith jewelThiefAsLocksmith = new JewelThief();'n");
            Locksmith jewelThiefAsLocksmith = new JewelThief();
            jewelThiefAsLocksmith.OpenSafe(safe, owner);
            Console.WriteLine("when ReturnContents() called from main,");
            Jewels j = safe.Open("12345");
            jewelThiefAsLocksmith.ReturnContents(j, owner);
            ///JewelThief jewelThief = new Locksmith(); is error
            Console.ReadKey();
        }
    }
    class Jewels
    {
        public string Sparkle()
        {
            return "Sparkle, sparkle!";
        }
    }
    class Safe
    {
        private Jewels contents = new Jewels();
        private string safeCombination = "12345";
        public Jewels Open(string combination)
        {
            if (combination == safeCombination)
                return contents;
            else
                return null;
        }
        public void PickLock(Locksmith lockpicker)
        {
            lockpicker.WriteDownCombination(safeCombination);
        }
    }
    class Owner
    {
        private Jewels returnedContents;
        public void ReceiveContents(Jewels safeContents)
        {
            returnedContents = safeContents;
            Console.WriteLine("Owner:Thank you for returning my jewels! " + safeContents.Sparkle());
        }
    }
    class Locksmith
    {
        public void OpenSafe(Safe safe, Owner owner)
        {
            safe.PickLock(this);
            Jewels safeContents = safe.Open(writtenDownCombination);
            this.ReturnContents(safeContents, owner);
        }
        private string writtenDownCombination = null;
        public void WriteDownCombination(string combination)
        {
            writtenDownCombination = combination;
        }
        public void ReturnContents(Jewels safeContents, Owner owner)
        {
            owner.ReceiveContents(safeContents);
        }
    }
    class JewelThief : Locksmith
    {
        private Jewels stolenJewels = null;
        public void ReturnContents(Jewels safeContents, Owner owner)
        {
            stolenJewels = safeContents;
            Console.WriteLine("JewelThief:I'm stealing the contents! " + stolenJewels.Sparkle());
        }
    }
}

上面的代码没有覆盖方法ReturnContents()。它在躲藏。所以我期待着该声明

。ReturnContents(老板safeContents);

存在于Locksmith类中(在OpenSafe()方法中),如果引用对象' Jewelthief ',将调用ReturnContents()方法present Jewelthief对象。

但是每次它调用基类方法,ReturnContents。如何解释这种行为?

隐藏基类中存在的方法

在c#中,你必须声明你的虚方法。也许你正在考虑JAVA?

将方法签名更改为:

public virtual void ReturnContents(Jewels safeContents, Owner owner)

为基类,以及:

public override void ReturnContents(Jewels safeContents, Owner owner)

为子类。

隐藏发生在子类重新定义基类已经定义的方法时。在这种情况下,运行时将调用引用类型的方法,而不使用多态性。如果需要这种行为,您应该在子类中使用"new"关键字标记该方法。然而,如果你想使用多态性,你需要将基类的方法声明为virtual,并在子类中为该方法使用"override"关键字。

当你想在基类中使用大部分代码时,你会使用隐藏,但你想在你的类中为某个方法提供新的行为,并将其暴露给你正在定义使用隐藏的所有子类。这将避免改变基类对方法的内部调用的行为,但仍然允许您为新类的用户提供新的实现。

如果您不想要Override方法,您可以使用new操作符隐藏它

    public new void ReturnContents(Jewels safeContents, Owner owner)
    {
        stolenJewels = safeContents;
        Console.WriteLine("JewelThief:I'm stealing the contents! " + stolenJewels.Sparkle());
    }