Using interface 3 : Interface « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » InterfaceScreenshots 
Using interface 3
Using interface 3

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System; 

// An encryption interface. 
public interface ICipher 
  string encode(string str)
  string decode(string str)
}

/* A simple implementation of ICipher that codes 
   a message by shifting each character 1 position 
   higher.  Thus, A becomes B, and so on. */ 
class SimpleCipher : ICipher 
     
  // Return an encoded string given plaintext. 
  public string encode(string str) { 
    string ciphertext = ""
 
    for(int i=0; i < str.Length; i++
      ciphertext = ciphertext + (char) (str[i1)
 
    return ciphertext; 
  
 
  // Return an decoded string given ciphertext. 
  public string decode(string str) { 
    string plaintext = ""
 
    for(int i=0; i < str.Length; i++
      plaintext = plaintext + (char) (str[i1)
 
    return plaintext; 
  

 
/* This implementation of ICipher uses bit 
   manipulations and key. */ 
class BitCipher : ICipher 
  ushort key; 
 
  // Specify a key when constructing BitCiphers. 
  public BitCipher(ushort k) { 
    key = k; 
  
     
  // Return an encoded string given plaintext. 
  public string encode(string str) { 
    string ciphertext = ""
 
    for(int i=0; i < str.Length; i++
      ciphertext = ciphertext + (char) (str[i^ key)
 
    return ciphertext; 
  
 
  // Return an decoded string given ciphertext. 
  public string decode(string str) { 
    string plaintext = ""
 
    for(int i=0; i < str.Length; i++
      plaintext = plaintext + (char) (str[i^ key)
 
    return plaintext; 
  
}

// Use ICipher. 
 
// A class for storing unlisted telephone numbers. 
class UnlistedPhone 
  string pri_name;   // supports name property 
  string pri_number; // supports number property 
 
  ICipher crypt; // reference to encryption object 
 
  public UnlistedPhone(string name, string number, ICipher c
  
    crypt = c; // store encryption object 
 
    pri_name = crypt.encode(name)
    pri_number = crypt.encode(number)
  
 
  public string Name 
    get 
      return crypt.decode(pri_name)
    
    set 
      pri_name = crypt.encode(value)
    
  
 
  public string Number 
    get 
      return crypt.decode(pri_number)
    
    set 
      pri_number = crypt.encode(value)
    
  

 
// Demonstrate UnlistedPhone 
public class UnlistedDemo 
  public static void Main() { 
    UnlistedPhone phone1 = 
        new UnlistedPhone("Tom""555-3456"new BitCipher(27));  
    UnlistedPhone phone2 = 
        new UnlistedPhone("Mary""555-8891"new BitCipher(9))
 
 
    Console.WriteLine("Unlisted number for " +  
                      phone1.Name + " is " 
                      phone1.Number);  
 
    Console.WriteLine("Unlisted number for " +  
                      phone2.Name + " is " 
                      phone2.Number);  
  
}


           
       
Related examples in the same category
1. Demonstrate the ByTwos interfaceDemonstrate the ByTwos interface
2. Demonstrate the ByTwos interface 2Demonstrate the ByTwos interface 2
3. Use a property in an interfaceUse a property in an interface
4. Add an indexer in an interfaceAdd an indexer in an interface
5. One interface can inherit anotherOne interface can inherit another
6. Explicitly implement an interface memberExplicitly implement an interface member
7. Use explicit implementation to remove ambiguityUse explicit implementation to remove ambiguity
8. Two class inherit one interfaceTwo class inherit one interface
9. illustrates interfacesillustrates interfaces
10. illustrates implementing multiple interfacesillustrates implementing multiple interfaces
11. illustrates inheriting from a class and implementing multiple interfacesillustrates inheriting from a class and implementing multiple interfaces
12. illustrates casting an object to an interfaceillustrates casting an object to an interface
13. illustrates deriving an interface from one interfaceillustrates deriving an interface from one interface
14. illustrates deriving an interface from multiple interfacesillustrates deriving an interface from multiple interfaces
15. illustrates an explicit interface member implementationillustrates an explicit interface member implementation
16. illustrates interface member hidingillustrates interface member hiding
17. Demonstrates the use of a simple interfaceDemonstrates the use of a simple interface
18. Interface demoInterface demo
19. Interface demo: implements two interfacesInterface demo: implements two interfaces
20. Interface castingInterface casting
21. Overriding InterfacesOverriding Interfaces
22. Overriding Interfaces: Tester Overriding InterfacesAsOverriding Interfaces: Tester Overriding InterfacesAs
23. A safe method of determining whether a class implements a particular interfaceA safe method of determining whether a class implements a particular interface
24. Interfaces:Working with InterfacesInterfaces:Working with Interfaces
w_w___w_.j_a___v_a2s_.c_om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.