Classes can derive from only one class (and if you don't explicitly derive from a class, then you implicitly derive from Object). Classes can implement any number of interfaces. When you design your class, you can choose not to implement any interfaces, you can implement a single interface, or you can implement two or more interfaces.
For example, in addition to IStorable, you might have a second interface, ICompressible, for files that can be compressed to save disk space. If your Document class can be stored and compressed, you might choose to have Document implement both the IStorable and ICompressible interfaces.
IStorable and ICompressible, implemented by Document
using System;
namespace InterfaceDemo
{
interface IStorable
{
void Read();
void Write(object obj);
int Status { get; set; }
}
// here's the new interface
interface ICompressible
{
void Compress();
void Decompress();
}
// Document implements both interfaces
public class Document : IStorable, ICompressible
{
// the document constructor
public Document(string s)
{
Console.WriteLine("Creating document with: {0}", s);
}
// implement IStorable
public void Read()
{
Console.WriteLine(
"Implementing the Read Method for IStorable");
}
public void Write(object o)
{
Console.WriteLine(
"Implementing the Write Method for IStorable");
}
public int Status
{
get { return status; }
set { status = value; }
}
// implement ICompressible
public void Compress()
{
Console.WriteLine("Implementing Compress");
}
public void Decompress()
{
Console.WriteLine("Implementing Decompress");
}
// hold the data for IStorable's Status property
private int status = 0;
}
class Tester
{
public void Run()
{
Document doc = new Document("Test Document");
doc.Status = -1;
doc.Read();
doc.Compress();
Console.WriteLine("Document Status: {0}", doc.Status);
}
[STAThread]
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
}
Output:
Creating document with: Test Document
Implementing the Read Method for IStorable
Implementing Compress
Document Status: -1
RELATED POST
UNIT TESTING PART ONE
UNIT TESTING PART TWO
UNIT TESTING PART THREE
GUI TESTING
WINDOWS COMPLIANCE GUI TESTING PART ONE
WINDOWS COMPLIANCE GUI TESTING PART TWO
WINDOWS COMPLIANCE GUI TESTING PART THREE
WINDOWS COMPLIANCE GUI TESTING PART FOUR VALIDATION TESTING
WINDOWS COMPLIANCE GUI TESTING PART FIVE CONDITION TESTING
WINDOWS COMPLIANCE GUI TESTING PART SIX GENERAL CONDITION TESTING
CONDITION TESTING
TESTING CONDITIONS PART ONE
TESTING CONDITIONS PART TWO
TESTING CONDITIONS PART THREE
TESTING CONDITIONS PART FOUR
SPECIFIC FIELD TESTING
USABILITY TESTING
INTEGRATION TESTING
INTEGRATION TESTING PART ONE
INTEGRATION TESTING PART TWO
INTEGRATION TESTING PART THREE
INTEGRATION TESTING PART FOUR
INTEGRATION TESTING PART FIVE
INTEGRATION TEST STANDARDS
INTEGRATION TEST STANDARDS PART TWO
No comments:
Post a Comment