C # hides confusion

Does this bother me, please tell me the behavior of this?

The announcement of a new member hides the inherited member only within the scope of the new member. Copy

**class Base
{
   public static void F() {}
}
class Derived: Base
{
   new private static void F() {}   // Hides Base.F in Derived only
}
class MoreDerived: Derived
{
   static void G() { F(); }         // Invokes Base.F
}**

In the above example, declaring an F in Derived hides an F that was inherited from Base, but since the new F in Derived has private access, its scope does not extend to MoreDerived. So the call to F () in MoreDerived.G is valid and will call Base.F.

I don’t understand how he static void G() { F(); }can access the method of the base class f, when he can access all the methods of his immediate superclass, and the superclass hides the method f of the base class

+5
source share
2 answers

MoreDerived ; , private. # , private , - . , private , - . new private static void F , MoreDerived .

protected , MoreDerived .

new . , . Derived - Base.F() :

class Derived: Base 
{ 
   new private static void F()
   {
       F();      // calls Derived.F()
       Base.F(); // calls Base.F()
   }
} 

class MoreDerived: Derived 
{ 
   static void G() 
   {
       F();         // Invokes Base.F
       Derived.F(); // Invokes Base.F because Derived.F is private
       Base.F();    // Invokes Base.F
   }
}

:

class Foo
{
    int bar; // in most methods, this variable can be accessed as just "bar"

    Foo(int bar) // this parameter will hide the instance member bar
    {
        this.bar = bar; // instance method can still be accessed, though
    }
}
+10

"" , .

- MoreDerived: Derived: Base, .

.

+1

All Articles