public class A { public static int a = 12; public virtual int g() { a--; return h(); } public virtual int h() { a--; return i(); } public int i() { return --a; } } class B : A { public int b = 12; public override int h() { b++; return (a--) + (b++); } public new int i() { return this.g(); } static void Main(string[] args) { Console.WriteLine("A: {0} {1} {2}", new A().g(), new A().h(), new A().i()); Console.WriteLine("B: {0} {1} {2}", new B().g(), new B().h(), new B().i()); Console.ReadLine(); } }
I found an example, and I was stuck. I know that he will print:
A: 9 7 6
but I donโt know why it prints:
B: 18 17 15
On track A she g() takes 12 and makes her 11, and then pushes her to h() - then she 10, i() makes her 9 even before she goes anywhere. So this is A : 9 for sure. Then it's the same again, so it's 9 7 and 6. However, on B : it takes the existing a (which is now 6), and g() makes it 5. Then B gets the increment to 13, then 5 + 13 = 18. After that, a = 5 changes to a = 4 and b = 14 . I understand it. But why is it 17 again next? Shouldn't be 18?
source share