Experiment on forced conversion between parent class and subclass in C #

Source: Internet
Author: User

MSDN is good, but sometimes you need to practice it yourself to better understand and remember something.

I have read a lot of technical articles and I still cannot get started with the results. It is similar.

Like the last "four homes, six homes, five homes, five homes, six homes, six homes, five homes, six homes, six homes, five single-doubles", it is still unclear to check MSDN, write a small example and you will understand it.

This time, I searched for "Conversion of the base class derived class" and read the result. It is not clear and I do not understand it.

So I wrote an example and gave it a try. However, the specific principles are unclear. If you have any friends who know the specific principles, I will give you some advice.

In order to save time for more friends who are still learning, just like me, here is an ugly show. If you are a master, please provide guidance and do not combat the enthusiasm of cainiao.

The Code contains detailed annotations, so the code is directly written.

Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;

Namespace WebApplication3
{

/// <Summary>
/// Class a intentionally defines two fields
/// </Summary>
Public class
{
Public string itema1 {get; set ;}
Public string itema2 {get; set ;}
Public string PrintItem ()
{
Return itema1 + itema2;
}
}
/// <Summary>
/// Class B intentionally defines one field, plus three fields inherited from Class.
/// </Summary>
Public class B:
{
Public string itemb1 {get; set ;}

Public string PrintItemb ()
{
Return itema1 + itema2 + itemb1;
}
}

Public class d:
{
Public string itemd1 {get; set ;}

Public string PrintItemd ()
{
Return itema1 + itema2 + itemd1;
}
}
Public class e: B
{
Public string iteme1 {get; set ;}

Public string PrintIteme ()
{
Return itema1 + itema2 + itemb1 + iteme1;
}
}

Public static class c
{
Public static a geta ()
{
Return getb ();
}

Public static B getb ()
{
B CBA = new B () {itemb1 = "bb1", itema1 = "bba1", itema2 = "bba2 "};
Return CBA;
}
}

Public partial class WebForm1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Next, let's test and instantiate Class a and Class B and assign them to different values.
A aa = new a () {itema1 = "a1", itema2 = "a2 "};
B bb = new B () {itemb1 = "b1", itema1 = "ba1", itema2 = "ba2 "};

A abc = (a) bb; // convert a bb instance with three fields to an abc instance of Class a with only two fields.
// Test whether or not a class a bag can only contain 2 steamed buns, and Class B bags can contain 3 steamed buns.
// The bb bag containing three steamed buns is now loaded into the abc bag containing only two steamed buns. Will it discard one steamed stuffed bun.
// The result shows that we will not discard the bag, but we can only take two steamed buns out of the abc bag. When we put the abc bag, when the new instance bbc bag is forcibly converted and loaded back into the B-class bag, the three steamed buns are returned.

B bbc = (B) abc;
// B dbb = (B) aa; // an error occurs here !! The message "cannot forcibly convert an object of the type" WebApplication3.a "to the type" WebApplication3. B"
// As compared to the preceding steamed stuffed bun, if you do not put the three steamed stuffed buns in the B-class bag into the-class bag first, you can directly convert the Class a bag to the Class B bag, that is not acceptable. It is feasible to start from B to a and then to B.
Label1.Text = aa. PrintItem ();
Label2.Text = bb. PrintItemb ();
Label3.Text = abc. PrintItem ();
Label4.Text = bbc. PrintItem ();
Label5.Text = bbc. PrintItemb ();
Label6.Text = (c. geta (). PrintItem (); // this way,
Label7.Text = (B) (c. geta (). PrintItemb. The conclusion is: the first three steamed stuffed buns, no matter which type they are installed, are "conservation of energy"/"the material is not destroyed,
// It can only be set in different classes according to the "Rules" of this class. In Class a, only two can be taken, and three can be taken out in Class B. The premise is that there are three steamed buns during initial instantiation.
// Label8.Text = (d) (c. geta (). PrintItemd (); // an error will occur here !! The message "cannot forcibly convert an object of the type" WebApplication3. B "to the type" WebApplication3.d"
// Label9.Text = (e) (c. geta (). PrintIteme (); // an error will occur here !! The message "cannot forcibly convert an object of the" WebApplication3. B "type to the" WebApplication3.e "type ".
// Note the prompt "the type cannot be" WebApplication3. B ", and c. geta () still returns Class B. Instead of a when the geta () method is defined.

}
}
/* The output result is:

A1a2
Ba1ba2b1
Ba1ba2
Ba1ba2
Ba1ba2b1
Bba1bba2
Bba1bba2bb1



*/

}

Http://four-corner.appspot.com/

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.