. NET Learning notes----2015-06-26 (polymorphic exercises)

Source: Internet
Author: User

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)

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.