If a method has the same name as the method inherited from the base class from the derived class, Therefore, if you want to hide the methods inherited from the underlying class, you can hide them through the new keyword.
class myTest
{
public void Bark()
{
Console.WriteLine ("Woof");
}
}
class Test : myTest
{
public void Bark()
{
Console.WriteLine ("Squeak");
}
}
Test nTest = new Test();
Test.Bark();
I said that I can hide the methods of the base class through the new keyword, so it's not hidden originally It's an act of hiding through the new keyword, so if you don't hide it like above,
I thought Test.Bark() would call the Bark method of the base class, but Bark() of the derived class is called, so isn't it the same effect as whether to use the new keyword or not? Is the result the same because the compiler automatically processed new?
I don't know what it means to cover.
If you didn't know the new keyword, of course, you'd run Test.Bark() Method of Test Class As you can predict, the new keyword means hiding, so if you don't use it, it's interpreted as not hiding it.
c#
Test nTest = newTest(); new has a different meaning. This new means that you are dynamically assigning instances of the Test class. The function of the new that you mentioned is used before the method or before the variable. Take the code above as an example:
class Test : myTest
{
new public void Bark()
{
Console.WriteLine ("Squeak");
}
}
In fact, the Bark() method is hidden even if you don't write it like this. However, it is clearly displayed by using the keyword new.
© 2024 OneMinuteCode. All rights reserved.