您能解释一下这个问题的输出吗? [英] can you please explain the output of this question?

查看:68
本文介绍了您能解释一下这个问题的输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {

            derived d = new derived();
            d.DoSome();
            Base b = d;
            b.DoSome();
        }                                    
                                             
    }

    class Base
    {
        public virtual void DoSome()
        {
            Console.WriteLine("In base class");
        }


   }

    class derived: Base
    {
        public override void DoSome()
        {
            Console.WriteLine("In Derived class");

        }

}


}

推荐答案

虽然很容易阅读代码以查看结果,但我建议您使用调试器实际查看会发生什么.

使用调试器编译项目(完成后),遍历每一行,您将直观地看到发生了什么,调用了什么以及输出了什么.

该示例(可能是由您的老师给出的)实际上还在将结果写到控制台(如果是控制台应用程序)或Visual Studio的输出窗口,因此也可以通过简单地运行该应用程序来查看逻辑.
While it''s quite easy to read the code in order to see the results, I suggest that you use a debugger to actually see what happens.

Compile the project (as you have done), using the debugger go through every line and you''ll see visually what happens and what is called and what is the output.

The sample (probably given by your teacher) is actually also writing the results to either the console (if it is a console app) or to the output window of Visual Studio so the logic can also be seen by simply running the app.


主要问题没有任何意义且无法回答,但我可以回答后续问题,请参见相关评论:
While the main question makes no sense and cannot be answered, I can answer the follow up question, see the comments to it:
成员9352152303问:
Member 9352152303 asked:

第二行将引用哪个DoSome方法?

which DoSome method will be refered to by the second line?

当然,将执行derived.DoSome再次,因为b的运行时类型是derived;在赋值b == d之后,两个变量都指向同一个对象.

理解它需要了解引用类型,继承以及编译时变量和运行时变量之间的差异.这个基本的东西还没有接近OOP的核心,它始于您覆盖虚拟/抽象方法/属性以实现 late绑定时.您需要很好地学习和理解所有内容才能进行任何形式的.NET编程.

请理解,提问的方式是无效的.您需要解释一些问题,而不是要求对该问题进行解释".

—SA

Of, course, derived.DoSome will be executed again, because the run-time type of b is derived; after the assignment b == d, both variables point to the same object.

Understanding of it required understanding of reference types, inheritance and difference between compile-time and run-time variables. This basic stuff not yet even close to the heart of OOP which begins when you override virtual/abstract methods/properties to achieve late binding. You will need to learn and understand all that very well to do any kind of .NET programming.

Please understand that the way of asking question is not productive. You need to explain some problem, instead of asking for "explanation" of the problem.

—SA



这是一篇很好的文章,可以帮助您了解代码中正在发生的事情.

六个重要的.NET概念:堆栈,堆,值类型,引用类型,装箱和拆箱 [
Hi,
Here is a good article that will help you understand what is happening in your code.

Six important .NET concepts: Stack, heap, value types, reference types, boxing, and unboxing[^]


这篇关于您能解释一下这个问题的输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆