首页 新闻 会员 周边 捐助

帮忙看看下面的代码问题 看看最下面的输出结果对不对 简要说明理由

0
悬赏园豆:20 [已解决问题] 解决于 2011-09-29 16:22

publicclass Test1

 

    {

 

        private int _age=10;

 

        public int Age

 

        {

 

            get { return _age; }

 

            set { _age = value; }

 

        }

 

        public virtual void  Output1(string name)

 

        {

 

            name ="Test1";

 

            Console.Write("Test1.Output1 :Name is "+ name);

 

        }

 

        public virtual void Output2(ref stringrefname)

 

        {

 

            refname = "Test1";

 

            Console.Write("Test1.Output2 :Name is " + refname);

 

        }

 

    }

 

    public class Test2:Test1

 

    {

 

        private int _age=20;

 

        public override void Output1(stringname)

 

        {

 

            name ="Test2";

 

            Console.Write("Test2.Output1 :Name is "+ name);

 

        }

 

        public new void Output2(ref stringrefname)

 

        {

 

            refname = "Test2";

 

            Console.Write("Test2.Output2 :Name is " + refname);

 

        }

 

        }

 

        string name = "Name";

 

        string refname = "RefName";

 

        Test1 t1 = new Test2();

 

        t1.Output1(name);  //输出1?

 

        t1.Output2(ref refname);  // 输出2?

 

        Console.Write(name); // 输出3?

 

        Console.Write(refname); // 输出4?

 

        Test2 t2 = new Test2();

 

        Console.Write(t2.Age.ToString()); //输出5以及是否装箱?

 

        t2.Output2(ref refname);  //输出6

Ganler1988的主页 Ganler1988 | 初学一级 | 园豆:3
提问于:2011-09-27 23:26
< >
分享
最佳答案
0

Test2类重写了Test1类的OutPut1方法,隐藏了OutPut2方法,因此t1.OutPut1(name)输出应该调用的是重写后的方法即t2中的方法,输出应为:est2.Output1 :Name is Test2;t1.OutPut2(ref refname)输出应该是:Test1.Output2 :Name is  Test1。Console.Write(name)输出为:Name;Console.Write(refName)应为Test2。Console.Write(t2.Age.ToString())不是很清楚。t2.Output2(ref refname)输出:Test2.Output2 :Name is  test2

收获园豆:20
华-子 | 初学一级 |园豆:156 | 2011-09-28 18:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册