0

my code is like this , how ever B.Log() will output base class Log2

I wonder that can new keyword hide it when function is called in base class?

I guess it's impossible as new is not as strong as override,zzzz

public class A
{
    public void Log()
    {
        Log2();
    }
    public void Log2()
    {
        Debug.LogError("AAAAAAAAAA");
    }
}

public class B : A
{
    public new void Log2()
    {
        Debug.LogError("BBBBBBBbb");
    }
}

I guess it's impossible as new is not as strong as override.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
fp Van
  • 1
  • 1
  • 4
    It's not a matter of "strong" - it's a matter of "it's declaring a new method instead of overriding an existing one". If you ran `new B().Log2();` from some code in A, that would still print BBBBBBBbb, but while you're calling `Log2` on a reference with a compile-time type of `A` (including the implicit `this` reference), then yes, it'll call the method in `A`. – Jon Skeet Nov 04 '22 at 07:52

0 Answers0