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