通过基类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() ?写()`?
I have this code but I just can't understand it.
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();
}
}
}
I thought Student.Write()
would be called in both cases, so I was puzzled by what I got:
// 1
Student.Read()
Person.Write()
// 2
Student.Read()
Person.Write()
Why is Person.Write()
called instead of 'Student.Write()`?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
new
关键字表明您不打算重写基类的Write()
方法(无论如何都不能这样做,因为Person
的Write()
方法未标记为virtual
)。由于您是通过IStoreable
调用它,因此IStoreable
接口没有将其链接到Student
类。由于Write()
未标记为virtual
,因此该函数的多态性不适用。The
new
keyword indicates that you do not intend to override the base class'sWrite()
method (you can't anyway, sincePerson
'sWrite()
method isn't markedvirtual
). Since you're calling it viaIStoreable
, there's nothing about theIStoreable
interface that links it to theStudent
class. SinceWrite()
is not markedvirtual
, polymorphism for this function doesn't apply.作为 IStoreable 的 Student 无法看到 Student.Write 方法,因为它不是从基类 Person 重写的。为什么它没有标记为 virtual,为什么使用 new 关键字来隐藏基类的实现?
Student as an IStoreable cannot see the Student.Write method since it is not overriden from the base class Person. Why is it not marked virtual, and why have you used the new keyword to hide the base class's implementation?
使该人的 write 方法 Virtual。当您将其标记为 new 时,它不会充当继承方法。当您将方法标记为虚拟时,这意味着您提供了一个实现并且可以由子类覆盖它。 Abstract 要求您实现一个方法(仅供参考)。
“当用作修饰符时,new 关键字显式隐藏从基类继承的成员”通过
Make the person's write method Virtual. When you mark it new it does not act as an inherited method. When you mark the method virtual that means you provide an implementation and can override it by a child class. Abstract requires you implementat a method (just a little more fyi).
"When used as a modifier, the new keyword explicitly hides a member inherited from a base class" via