/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_1.cs illustrates interfaces
*/
using System;
// define the IDrivable interface
publicinterface IDrivable
{
// method declarations
void Start();
void Stop();
// property declaration
bool Started
{
get;
}
}
// Car class implements the IDrivable interface
class Car : IDrivable
{
// declare the underlying field used by the Started property
private bool started = false;
// implement the Start() method
publicvoid Start()
{
Console.WriteLine("car started");
started = true;
}
// implement the Stop() method
publicvoid Stop()
{
Console.WriteLine("car stopped");
started = false;
}
// implement the Started property
public bool Started
{
get
{
return started;
}
}
}
publicclass Example8_1
{
publicstaticvoid Main()
{
// create a Car object
Car myCar = new Car();
// call myCar.Start()
myCar.Start();
Console.WriteLine("myCar.Started = " + myCar.Started);
// call myCar.Stop()
myCar.Stop();
Console.WriteLine("myCar.Started = " + myCar.Started);
}
}