日期:2014-05-18  浏览次数:20798 次

不用new作为修饰符怎么也没问题。。
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            B b = new B();
            Console.WriteLine(b.x);
            Console.Read();
        }
        
    }
         public class A {
            public int x ;
            
        }
        public class B:A{
            public int x;  //A包含x成员,B类也包含x成员,并特意使用new修饰符隐藏A类的x成员。
    
        }

}

书上说要用new隐藏掉相同的属性,我没用new为什么还是对的,没警告没错误,
而且试了几个值,系统找的都是B中的x……
再者怎么才能找到A的x。。。

------解决方案--------------------
1.警告是有的
2.编译器 隐式帮你加new了
3.A b = new B();输出A的
------解决方案--------------------
Console.WriteLine((b as A).x);