Using polymorphic to achieve USB, MP3, mobile hard disk and computer docking, read write data.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.