1.在子类中隐藏基类成员不会移除基类成员,只是使基类成员在子类中不可见
2.在子类B中申明的一个新成员隐藏了基类A中的成员时,只能在新成员的范围内隐藏基类A的成员。如果新成员为private,那么,B的派生类C中,C将继承A的成员,而不是B的成员。
- using System;
- using System.Collections.Generic;
- using System.Text;
- //隐藏方法的继承关系
- namespace hide
- {
- class Program
- {
- class Base
- {
- protected static String name = "Base";
- public static void F()
- {
- Console.WriteLine("Base::F()");
- Console.WriteLine("name:{0}",name);
- }
- }
- class Derived : Base
- {
- new private static string name = "Dirved";//隐藏基字段,私有
- new protected static void F() //隐藏Base.F();
- {
- Console.WriteLine("name:{0}", name);
- Console.WriteLine("Dirved::F()");
- }
- public static void G()
- {
- F();
- name = "Dirved";
- Console.WriteLine("name:{0}", name);
- }
- }
- class MoreDirved : Derived
- {
- public static void H()
- {
- F();
- Console.WriteLine("name:{0}", name);
- }
- }
- static void Main(string[] args)
- {
- Base.F();
- Derived.G();
- MoreDirved.H(); //不显示Dirved的新私有字段
- Console.ReadKey();
- }
- }
- }
- /*
- Base::F()
- name:Base
- Dirved::F()
- name:Dirved
- Dirved::F()
- name:Base
- */