usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceMulti-state analog mobile HDD and u disk {classProgram {Static voidMain (string[] args) { //using polymorphic to achieve USB, MP3, mobile hard disk and computer docking, read write data. Mobiletool MT =NewUdisk (); Computer CPU=Newcomputer (); Cpu. Cpuread (MT); Cpu. Cpuwrite (MT); Console.readkey (); } /// <summary> ///the abstract parent class/// </summary> Public Abstract classMobiletool { Public Abstract voidRead (); Public Abstract voidWrite (); } Public classUdisk:mobiletool { Public Override voidRead () {Console.WriteLine ("USB Stick Read Success"); } Public Override voidWrite () {Console.WriteLine ("USB Drive Write Success"); } } Public classMp3disk:mobiletool { Public Override voidRead () {Console.WriteLine ("MP3 Read Success"); } Public Override voidWrite () {Console.WriteLine ("Mp3 Write Success"); } Public voidPlaymuisc () {Console.WriteLine ("Mp3 can play music himself"); } } Public classMobiledisk:mobiletool { Public Override voidRead () {Console.WriteLine ("Mobile Hard disk read Success"); } Public Override voidWrite () {Console.WriteLine ("Mobile Hard Disk write success"); } } Public classComputer { Public voidCpuread (Mobiletool mt) {Mt. Read (); } Public voidCpuwrite (Mobiletool mt) {Mt. Read (); } } }}
There are several ways to crawl a parent class in computer: You can pass a parameter fetch in a method, either through a constructor or through a property.
Static voidMain (string[] args) {Mobiletool MT=NewUdisk (); Computer CPU=Newcomputer (); Cpu. Mt=MT; Cpu. Cpuread (); Cpu. Cpuwrite (); Console.readkey (); } Public classComputer {PrivateMobiletool _MT; InternalMobiletool Mt {Get{return_MT;} Set{_MT =value;} } Public voidCpuread () {mt.read (); } Public voidCpuwrite () {mt.read ();
Using polymorphic to achieve USB, MP3, mobile hard disk and computer docking, read write data.