为了账号安全,请及时绑定邮箱和手机立即绑定

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

/ 猿问

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

慕村225694 2018-12-07 04:26:21

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

查看完整描述

1 回答

?
慕姐8265434

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

查看完整回答
反对 回复 2019-01-21

添加回答

回复

举报

0/150
提交
取消
意见反馈 邀请有奖 帮助中心 APP下载
官方微信