Using polymorphic simulation, the mobile hard disk, u disk, MP3 plugged into the computer for reading and writing data
Parent class: Abstract class, because do not know what kind of mobile device plugged into the computer, it is not clear how to read and write
Abstract class Mobilestorage { publicabstractvoid Read (); Public Abstract void Write (); }
Mobile HDD Class:
class Mobiledisk:mobilestorage { publicoverridevoid Read () { Console.WriteLine (" Mobile Hard drive is reading data ") ; } Public Override void Write () { Console.WriteLine (" mobile Hard disk is writing data ");} }
USB flash Drive Class:
class Udisk:mobilestorage { publicoverridevoid Read () { Console.WriteLine (" USB Stick is reading the data "); } Public Override void Write () { Console.WriteLine ("USB stick is writing data ");} }
MP3 class:
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 is playing music"); } }
Computer class:
classComputer {//obtaining a parent class by defining a field PrivateMobilestorage _ms; Publicmobilestorage Ms {Get{return_ms;} Set{_ms =value;} } Public voidCpuread () {ms.read (); } Public voidCpuwrite () {ms.write (); } //To get the parent class by using the pass parameter//Public void Cpuread (mobilestorage ms)//{ //Ms. Read (); //} //Public void Cpuwrite (mobilestorage ms)//{ //Ms. Write (); //}}
Main:
Static voidMain (string[] args) { //using polymorphic simulation, the mobile hard disk, u disk, MP3 plugged into the computer for reading and writing data//Mobiledisk MD = new Mobiledisk (); //udisk u = new Udisk (); //MP3 mp3 = new MP3 (); //Computer CPU = new computer (); //CPU. Cpuread (U); //CPU. Cpuwrite (u);Mobilestorage Ms=NewMobiledisk ();//new Udisk ();//new MP3 ();Computer CPU =Newcomputer (); Cpu. Ms=MS; Cpu. Cpuread (); Cpu. Cpuwrite (); //CPU. Cpuread (MS); //CPU. Cpuwrite (MS);Console.readkey (); }
. NET Learning notes----2015-06-26 (polymorphic exercises)