通过基类c#实现的接口调用类的方法

发布于 2024-12-26 10:04:31 字数 1400 浏览 3 评论 0原文

我有这段代码,但我就是无法理解。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小耗子 2025-01-02 10:04:31

new 关键字表明您不打算重写基类的 Write() 方法(无论如何都不能这样做,因为 PersonWrite() 方法未标记为virtual)。由于您是通过 IStoreable 调用它,因此 IStoreable 接口没有将其链接到 Student 类。由于 Write() 未标记为 virtual,因此该函数的多态性不适用。

The new keyword indicates that you do not intend to override the base class's Write() method (you can't anyway, since Person's Write() method isn't marked virtual). Since you're calling it via IStoreable, there's nothing about the IStoreable interface that links it to the Student class. Since Write() is not marked virtual, polymorphism for this function doesn't apply.

十秒萌定你 2025-01-02 10:04:31

作为 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?

夜唯美灬不弃 2025-01-02 10:04:31

使该人的 write 方法 Virtual。当您将其标记为 new 时,它不会充当继承方法。当您将方法标记为虚拟时,这意味着您提供了一个实现并且可以由子类覆盖它。 Abstract 要求您实现一个方法(仅供参考)。

class Person : IStoreable { 
    public virtual void Read() { Console.WriteLine("Person.Read()"); } 
    public virtual void Write() { Console.WriteLine("Person.Write()"); } 
} 
class Student : Person { 
    public override void Read() { Console.WriteLine("Student.Read()"); } 
    public override void Write() { Console.WriteLine("Student.Write()"); } 

“当用作修饰符时,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).

class Person : IStoreable { 
    public virtual void Read() { Console.WriteLine("Person.Read()"); } 
    public virtual void Write() { Console.WriteLine("Person.Write()"); } 
} 
class Student : Person { 
    public override void Read() { Console.WriteLine("Student.Read()"); } 
    public override void Write() { Console.WriteLine("Student.Write()"); } 

"When used as a modifier, the new keyword explicitly hides a member inherited from a base class" via

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文