Chinese Enterprise C # Object-oriented--inheritance

Source: Internet
Author: User
Tags dname

 Public classShengwu {Private string_name;  Public stringName {Get{return_name;} Set{_name =value;} }        protected stringOld="1231";  Public voidshengming () {Console.WriteLine ("living creatures are alive.");        Console.WriteLine (old); }            }
 classDongwu:shengwu {Private string_dname;  Public stringDname {Get{return_dname;} Set{_dname =value;} }         Public voidHuidong () {Console.WriteLine ("animals can move ."); }    }
  class // Mammals     {            }
class Plant:shengwu  // plant     {        publicvoid   Guanghe ()    {        Console.WriteLine (" photosynthesis ");    }    }
namespaceChinese Enterprise C Object-oriented __ Inheritance {classProgram {Static voidMain (string[] args) {DONGWU Data=NewDongwu ();            Data.shengming (); Data. Name="Biological Name"; Data. Dname="Animal Name"; Mammal Ma=Newmammal ();            Ma.huidong (); ObjectOData = data;//all types of base classes, all types are subclasses of objectShengwu sdata= data;//subclasses can be converted directly to the parent class, but the parent class does not have the properties and methods of the child class. Console.WriteLine (sdata. Name);//You can only point out the name, not the dname, because name is the creature itself, and dname is the individual property of the mammal. //Parent Class The rotor class has the condition that the object of the parent class must be transformed by the object of a subclass to be converted back. Dongwu DW =NewDongwu (); SHENGWU SW=DW; Dongwu DDW=(DONGWU) SW; Console.WriteLine (DDW.             Name); //because the essence of P is a creature, it cannot be converted to a plant.            For example, the essence of P is that plants (formerly plants, which are still plants after they are converted to organisms) can be converted to plants. //Shengwu p = new Shengwu (); //Plant xdata = (Plant) p; //Console.WriteLine (xdata. Name);Console.ReadLine (); }    }}

Chinese Enterprise C # Object-oriented--inheritance

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.