2
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()。它在隐藏。所以我期待着声明

this.ReturnContents(safeContents, owner);

存在于 Locksmith 类中(在 OpenSafe() 方法内),如果从对象“JewelThief”引用,将调用 ReturnContents() 方法存在 Jewelthief 对象。

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

4

2 回答 2

4

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

将您的方法签名更改为:

public virtual void ReturnContents(Jewels safeContents, Owner owner)

对于基类,并且:

public override void ReturnContents(Jewels safeContents, Owner owner)

为孩子班。

当子类重新定义基类已经定义的方法时,就会发生隐藏。在这种情况下,运行时将调用被引用类型的方法而不使用多态性。如果需要这种行为,您应该使用“new”关键字标记子类中的方法。但是,如果您想使用多态性,则需要将基类的方法声明为虚拟方法,并为子类中的方法使用“override”关键字。

如果您想使用基类中的大部分代码,但您希望在类中为某个方法具有新行为并将其公开给您定义的方法的所有子类,您将使用隐藏使用隐藏。这将避免改变基类对方法的内部调用的行为,但仍允许您为新类的用户提供新的实现。

于 2013-03-04T05:39:37.377 回答
0

如果您不想使用Override该方法,可以使用new运算符将​​其隐藏

    public new void ReturnContents(Jewels safeContents, Owner owner)
    {
        stolenJewels = safeContents;
        Console.WriteLine("JewelThief:I'm stealing the contents! " + stolenJewels.Sparkle());
    }
于 2013-03-04T05:43:58.040 回答