class // Create an Electrical class: parent class { privatestring _dianqimingzi; Public string Dianqimingzi { getreturn _dianqimingzi;} Set {_dianqimingzi = value;} } }
class computer:dianqi // Create a computer class: Subclass { private string _diannaomingzi; public string Diannaomingzi { get {return _diannaomingzi;} set {_diannaomingzi =
class Lenovo:computer // Create Lenovo Class: Subclass of Subclass { privatestring _lname; Public string Lname { getreturn _lname;} Set {_lname = value;} } }
namespaceInherit practice{classProgram {Static voidMain (string[] args) { //Appliances: Parent classDianqi Dianqi1 =NewDianqi (); Dianqi1. Dianqimingzi="Electrical Appliances"; Console.WriteLine ("the name of the appliance is"+Dianqi1. Dianqimingzi); //Computer: Sub-categoryComputer Diannao =Newcomputer (); Diannao. Diannaomingzi="Computer"; Console.WriteLine ("the name of the computer is"+Diannao. Diannaomingzi); //computers that inherit electrical propertiesDiannao. Dianqimingzi ="A computer that inherits electrical properties (subclasses inherit the properties of the parent's electrical name)"; Console.WriteLine ("computer subclass inherit electrical Appliance the property after the parent class is"+Diannao. Dianqimingzi); //Lenovo: Sub-class of subclassesLenovo le =NewLenovo (); Le. Lname="Lenovo Computer"; Console.WriteLine ("the name of Lenovo Computer is"+le. Lname); //Lenovo that inherits the properties of the computerLenovo Le1 =NewLenovo (); Le1. Diannaomingzi="Lenovo Computer"; Le1. Dianqimingzi="The computer under the appliance is Lenovo"; Console.WriteLine ("the name of the computer property is"+Le1. Diannaomingzi); Console.WriteLine ("the name of the electrical property is"+Le1. Dianqimingzi); //Subclass to Parent class: Subclasses inherit the parent class, a subclass can have only one parent class, and a parent class may have more than one child class. //subclasses have the functions and properties of the parent class, and the parent class does not have subclasses. //a subclass can be converted to a parent class, but the parent class does not have properties and methods for the child class. //For example, a pig can be called a creature, but a creature cannot be called a pig. Computer Com =NewComputer ();//sub-class computerDianqi DCom = Com;//sub-class computer converted to Parent applianceDcom.dianqimingzi ="the name of the sub-class computer converted to the parent appliance"; Console.WriteLine (Dcom.dianqimingzi); //the parent class can be converted to subclasses, but there are prerequisites. Premise: Before the parent class is a subclass, it can be converted to the corresponding subclass. Computer AAA =NewComputer ();//Create a subclass of AAADianqi DDD = AAA;//converts a subclass AAA to a parent class DDDComputer XXX = (computer) DDD;//Convert the parent class DDD to subclass XXXXxx. Dianqimingzi ="parent class converted to child class"; Console.WriteLine (XXX. Dianqimingzi); //The following is a non- conforming situation where the code is error-free and runs incorrectly. //Dianqi ddd1 = new Dianqi (); //Computer xxx1 = (computer) ddd1; //xxx1. Dianqimingzi = "Parent class is converted to subclass (not conforming to the condition)"; //Console.WriteLine (xxx1. Dianqimingzi);Console.ReadLine (); } }}
Chinese Enterprise C # Object-oriented--inheriting practice