[Csharp]
/* Questions about how to output virtual, overried, and new methods and attributes in the form of this and base
* What is the output of the main program?
*/
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
A a1 = new B ();
A1.AM1 ();
B b1 = (B) a1;
B1.AM1 ();
Console. WriteLine ();
A a2 = new B ();
A2.AM2 ();
B b2 = (B) a2;
B2.AM2 ();
}
}
Class
{
Public virtual int I {get; protected set ;}
Public ()
{
This. I = 9;
}
Public virtual void AM1 ()
{
This. I = this. I + 10000;
Console. WriteLine ("A. this. I =" + this. I );
}
Public virtual void AM2 ()
{
This. I = this. I + 10000;
Console. WriteLine ("A. this. I =" + this. I );
}
}
Class B:
{
Public override int I {get; protected set ;}
Public B ()
: Base ()
{
This. I = 5;
}
Public override void AM1 ()
{
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
This. I = this. I + 100;
Base. I = base. I ++ 1000;
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
}
Public new void AM2 ()
{
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
This. I = this. I + 100;
Base. I = base. I ++ 1000;
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
}
}
}
/* Output
B. this. I = 5
B. base. I = 0
B. this. I = 105
B. base. I = 1000
B. this. I = 105
B. base. I = 1000
B. this. I = 205
B. base. I = 2000
A. this. I = 10005
B. this. I = 10005
B. base. I = 0
B. this. I = 10105
B. base. I = 1000
*/
/* Questions about how to output virtual, overried, and new methods and attributes in the form of this and base
* What is the output of the main program?
* After some logs are added
*/
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
A a1 = new B ();
A1.AM1 ();
B b1 = (B) a1;
B1.AM1 ();
Console. WriteLine ();
A a2 = new B ();
A2.AM2 ();
B b2 = (B) a2;
B2.AM2 ();
}
}
Class
{
Private int tmp;
Public virtual int I
{
Get
{
Console. WriteLine ("A. I. get =" + this. tmp );
Return this. tmp;
}
Protected set
{
Console. WriteLine ("A. I. set =" + value );
This. tmp = value;
}
}
Public ()
{
This. I = 9;
}
Public virtual void AM1 ()
{
This. I = this. I + 10000;
Console. WriteLine ("A. this. I =" + this. I );
}
Public virtual void AM2 ()
{
This. I = this. I + 10000;
Console. WriteLine ("A. this. I =" + this. I );
}
}
Class B:
{
Private int tmp;
Public override int I
{
Get
{
Console. WriteLine ("B. I. get =" + this. tmp );
Return this. tmp;
}
Protected set
{
Console. WriteLine ("B. I. set =" + value );
This. tmp = value;
}
}
Public B ()
: Base ()
{
This. I = 5;
}
Public override void AM1 ()
{
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
This. I = this. I + 100;
Base. I = base. I ++ 1000;
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
}
Public new void AM2 ()
{
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
This. I = this. I + 100;
Base. I = base. I ++ 1000;
Console. WriteLine ("B. this. I =" + this. I );
Console. WriteLine ("B. base. I =" + base. I );
}
}
}
/* Output
B. I. set = 9
B. I. set = 5
B. I. get = 5
B. this. I = 5
A. I. get = 0
B. base. I = 0
B. I. get = 5
B. I. set = 105
A. I. get = 0
A. I. set = 1000
B. I. get = 105
B. this. I = 105
A.io. get = 1000
B. base. I = 1000
B. I. get = 105
B. this. I = 105
A.io. get = 1000
B. base. I = 1000
B. I. get = 105
B. I. set = 205
A.io. get = 1000
A. I. set = 2000
B. I. get = 205
B. this. I = 205
A.io. get = 2000
B. base. I = 2000
B. I. set = 9
B. I. set = 5
B. I. get = 5
B. I. set = 10005
B. I. get = 10005
A. this. I = 10005
B. I. get = 10005
B. this. I = 10005
A. I. get = 0
B. base. I = 0
B. I. get = 10005
B. I. set = 10105
A. I. get = 0
A. I. set = 1000
B. I. get = 10105
B. this. I = 10105
A.io. get = 1000
B. base. I = 1000
*/