首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

一路简单笔试题

2013-10-27 
一道简单笔试题死心了没,LZ还是去看看继承,这个就是个基础来的,期末考试会出的题,没想到是面试题····[解决

一道简单笔试题


死心了没,LZ还是去看看继承,这个就是个基础来的,期末考试会出的题,没想到是面试题····
[解决办法]

死心了没,LZ还是去看看继承,这个就是个基础来的,期末考试会出的题,没想到是面试题····

一路简单笔试题
我用的是vs2008,你的呢?
道理上确实应该先执行A的构造函数,而且调用的是B的重写方法,应该输出像你说的那样,但我的机器怎么就是这样??
不关是VS08或者10的问题的,我的也是10的,是你代码问题吧,反正我是复制你的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            B b1 = new B();
            b1.PrintFields();
            Console.ReadLine();
        }
    }
    class A
    {
        public A()
        {
            PrintFields();
        }
        public virtual void PrintFields()
        {
        }
    }

    class B : A
    {
        int x = 1;
        int y;

        public B()
        {
            y = -1;


        }

        public override void PrintFields()
        {
            Console.WriteLine("x={0},y={1}", x, y);
        }
    }
}

[解决办法]


死心了没,LZ还是去看看继承,这个就是个基础来的,期末考试会出的题,没想到是面试题····

一路简单笔试题
我用的是vs2008,你的呢?
道理上确实应该先执行A的构造函数,而且调用的是B的重写方法,应该输出像你说的那样,但我的机器怎么就是这样??
不关是VS08或者10的问题的,我的也是10的,是你代码问题吧,反正我是复制你的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            B b1 = new B();
            b1.PrintFields();
            Console.ReadLine();
        }
    }
    class A
    {
        public A()
        {
            PrintFields();
        }
        public virtual void PrintFields()
        {
        }
    }

    class B : A
    {
        int x = 1;
        int y;

        public B()
        {
            y = -1;
        }

        public override void PrintFields()
        {
            Console.WriteLine("x={0},y={1}", x, y);
        }
    }
}

还有就是不要再纠结在这了,反正都面试不过了,还是想想下次面试吧

楼主只是说B b = new B();
并没有b.PrintFields();

所以你们结果不一样,去掉后面一行,你们就一致了。

热点排行