(C #) a question about polymorphism that seems simple but difficult to answer

Source: Internet
Author: User

[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
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.