using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Hello_World
{
interface IStorable
{
void Read();
void Write();
} public class Document : IStorable
{
public Document(string s)
{
Console.WriteLine("{0}",s);
} public virtual void Read()
{
Console.WriteLine("Document_Read");
} public virtual void Write()
{
Console.WriteLine("Document_Write");
}
} public class Note : Document
{
public Note(string s):base(s)
{
Console.WriteLine("this is Note");
} public override void Read()
{
//base.Read();
Console.WriteLine("Note_Read");
} public new void Write()
{
//base.Write();
Console.WriteLine("Note_write");
}
} class Program
{
static void Main(string[] args)
{
Note note2 = new Note("seond test");
IStorable isNote2 = note2 as IStorable;
if (isNote2 != null)
{
//isNote2.Read();
isNote2.Write();
}
//5note2.Read();
note2.Write();
}
}
}
这个是我根据书上的例子简化的代码,我想知道为什么两个 Write 方法的输出不一样,为什么接口的write方法调用的是基类方法?请帮忙解惑。
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Hello_World
{
interface IStorable
{
void Read();
void Write();
} public class Document : IStorable
{
public Document(string s)
{
Console.WriteLine("{0}",s);
} public virtual void Read()
{
Console.WriteLine("Document_Read");
} public virtual void Write()
{
Console.WriteLine("Document_Write");
}
} public class Note : Document
{
public Note(string s):base(s)
{
Console.WriteLine("this is Note");
} public override void Read()
{
//base.Read();
Console.WriteLine("Note_Read");
} public new void Write()
{
//base.Write();
Console.WriteLine("Note_write");
}
} class Program
{
static void Main(string[] args)
{
Note note2 = new Note("seond test");
IStorable isNote2 = note2 as IStorable;
if (isNote2 != null)
{
//isNote2.Read();
isNote2.Write();
}
//5note2.Read();
note2.Write();
}
}
}
这个是我根据书上的例子简化的代码,我想知道为什么两个 Write 方法的输出不一样,为什么接口的write方法调用的是基类方法?请帮忙解惑。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货