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

不用new作为修饰符如何也没有关问题。

2012-04-20 
不用new作为修饰符怎么也没问题。。C# codeusing Systemusing System.Collections.Genericusing System.Li

不用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);

热点排行