私はこのコードを持っていますが、私はそれを理解できません。基本クラスによって実装されたインターフェイスを介してクラスのメソッドを呼び出すC#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
interface IStoreable {
void Read();
void Write();
}
class Person : IStoreable {
public virtual void Read() { Console.WriteLine("Person.Read()"); }
public void Write() { Console.WriteLine("Person.Write()"); }
}
class Student : Person {
public override void Read() { Console.WriteLine("Student.Read()"); }
public new void Write() { Console.WriteLine("Student.Write()"); }
}
class Demo {
static void Main(string[] args) {
Person s1 = new Student();
IStoreable isStudent1 = s1 as IStoreable;
// 1
Console.WriteLine("// 1");
isStudent1.Read();
isStudent1.Write();
Student s2 = new Student();
IStoreable isStudent2 = s2 as IStoreable;
// 2
Console.WriteLine("// 2");
isStudent2.Read();
isStudent2.Write();
Console.ReadKey();
}
}
}
私はStudent.Write()
は両方のケースで呼ばれることだろうと思ったので、私は私が得たもので困惑した
// 1
Student.Read()
Person.Write()
// 2
Student.Read()
Person.Write()
はなぜPerson.Write()
ではなく、「Student.Write() `と呼ばれているのですか?
オーバーライドとシャドーイングは異なるものです。 http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c – Oded