//Multi-state analog mobile hard disk inserted into the computer read and write, Method 1 parameter, Method 2 property assignmentusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) { //Multi-state analog mobile hard drive plugged into computer read/write//Mobiledisk MD = new Mobiledisk ();//the parent class abstract class cannot be instantiated, so instantiating subclasses//Udisk ud = new Udisk (); //MP3 MP = new Mp3 ();Mobilestorage MP =NewMp3 (); Computer CPU=NewComputer ();//by attributes to the parent class, outside the argument can not be passed, with the assignmentCpu. Ms = MP;//indicates that the inserted MP3 is assigned to the MS property inside the computer.//CPU. Cpuread (MP); //CPU. Cpuwrite (MP);Cpu. Cpuread ();//Use the attribute assignment form without passing parametersCPU. Cpuwrite (); Console.readkey (); } } /// <summary> ///abstract parent class, Removable Storage/// </summary> Public Abstract classMobilestorage { Public Abstract voidRead (); Public Abstract voidWrite (); } Public classMobiledisk:mobilestorage { Public Override voidRead ()//Override can override abstract methods and virtual methods{Console.WriteLine ("The removable hard drive is reading data"); } Public Override voidWrite () {Console.WriteLine ("The removable hard drive is writing data"); } } Public classUdisk:mobilestorage { Public Override voidRead () {Console.WriteLine ("USB drive is reading data"); } Public Override voidWrite () {Console.WriteLine ("USB drive is writing data"); } } Public classMp3:mobilestorage { Public Override voidRead () {Console.WriteLine ("MP3 reading the data"); } Public Override voidWrite () {Console.WriteLine ("MP3 writing the data"); } Public voidPlaymusic () {Console.WriteLine ("MP3 can play music himself"); } } Public classComputer {//Method 2, take the word Gencun the passed-in parent class parameter PrivateMobilestorage _ms; PublicMobilestorage Ms//Field Encapsulation Properties { Get{return_ms;} Set{_ms =value;} } Public voidCpuread ()//Method 1, want to get the parent class, pass parameters in the line, you can pass the sub-class come in{ms.read ();//Method 2, through the property to get the parent class, outside the argument is not passed, using the assignment } Public voidCpuwrite () {ms.write (); } //Public void Cpuread (mobilestorage ms)//Method 1, want to get the parent class, pass parameters in the line, you can pass the sub-class come in//{ //Ms. Read (); //} //Public void Cpuwrite (mobilestorage ms)//{ //Ms. Write (); //} }}
Multi-state analog mobile hard disk inserted into the computer read and write, Method 1 parameter, Method 2 property assignment