Simple Object Interaction in C #

Source: Internet
Author: User

In the object of the world, everything is the object, object and object are independent of each other, but under the action of a certain force, the object began to work together

Instance of object interaction

TV sets everyone has it, according to the thinking mode of the object of everything, television can be a class, and then the TV has some basic methods such as:

TV Class TV

1. Turn on the TV Method open ();

2. How to turn off the TV close ();

3. Change the way the TV channel changes ();

 classTV {//the state of the TV        BOOLFlag; //How to turn on the TV         Public voidOpen () {if(flag) {//if the TV is on, it cannot be opened again .            }            Else{Console.WriteLine ("The TV has been turned on"); Flag=true; }        }        //How to turn off the TV         Public voidClose () {if(flag) {Console.WriteLine ("The TV is off ."); Flag=false; }            Else            {                //if the TV is off, you can't shut it down again .                           }        }        //ways to switch channels         Public voidChangestringname) {            if(flag) {Console.WriteLine ("a TV switch channel to"+name);            }            Else            {                //if the TV is off, you can't change the station .            }        }    }

People think that it is not particularly troublesome to switch the TV manually, and then there is the TV remote:

Remote Control class RC

1. Turn on the TV Method open ();

2. How to turn off the TV close ();

3. Change the way the TV channel changes ();

class RC    {        // Open button         Public void Open ()        {        }        // Close button         Public void Close ()        {        }        // Change button         Public void Change ()        {        }    }

How can we do that, press the remote control button will be able to do the corresponding operation of the TV? Is it possible to bind the TV's method to the remote control?

The code is as follows:

 classRC {//declare an object of a TV class and accept the TV object to be remotely controlledTV TV; //The object of the TV to the remote control, is the universal remote control of the process of binding TV         Public voidbinding (TV TV) { This. TV =TV; }        //Open Button         Public voidOpen () {tv.open (); }        //Close button         Public voidClose () {TV.        Close (); }        //Change the table button         Public voidChangestringname)        {Tv.change (name); }    }

Then you can use the remote control TV!!

Static voidMain (string[] args) {            //buy a TVTV TV =NewTV (); //take out the remote .RC rc =NewRC (); //bind the remote control to the TVrc.binding (TV); //turn on the TVRc.open (); //change a station .Rc.change ("CCTV"); //turn off the TVRC.            Close ();        Console.ReadLine (); }

Operation Result:

People are not < object interaction > have a certain understanding of it;

Simple Object Interaction in C #

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.