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 #