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对象。
但每次调用baseclass方法时,ReturnContents。如何解释这种行为?
答案 0 :(得分:4)
在C#中,您必须声明您的虚拟方法。也许你在考虑JAVA?
将您的方法签名更改为:
public virtual void ReturnContents(Jewels safeContents, Owner owner)
表示基类,并且:
public override void ReturnContents(Jewels safeContents, Owner owner)
为儿童班。
当子类重新定义基类已定义的方法时,会发生隐藏。在这种情况下,运行时将调用引用类型的方法而不使用多态。如果需要此行为,则应使用“new”关键字在子类中标记该方法。但是,如果要使用多态,则需要将基类的方法声明为virtual,并对子类中的方法使用“override”关键字。
如果你想在基类中使用大部分代码,你会使用隐藏,但是你希望在你的类中为某个方法创建新的行为并将其公开给你所有的子类。正在定义使用隐藏。这将不会改变基类的内部行为调用方法,但仍允许您为新类的用户提供新的实现。
答案 1 :(得分:0)
如果您不想使用Override
方法,可以使用new
运算符隐藏它
public new void ReturnContents(Jewels safeContents, Owner owner)
{
stolenJewels = safeContents;
Console.WriteLine("JewelThief:I'm stealing the contents! " + stolenJewels.Sparkle());
}