Demonstrate the ByTwos interface : Interface : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » Interface »

 

Demonstrate the ByTwos interface









    

/*
C#: The Complete Reference 
by Herbert Schildt 

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

public interface ISeries { 
  int getNext(); // return next number in series 
  void reset(); // restart 
  void setStart(int x); // set starting value 
}

// Implement ISeries. 
class ByTwos : ISeries { 
  int start; 
  int val; 
 
  public ByTwos() { 
    start = 0; 
    val = 0; 
  }  
 
  public int getNext() { 
    val += 2; 
    return val; 
  } 
 
  public void reset() { 
    val = start; 
  } 
 
  public void setStart(int x) { 
    start = x; 
    val = start; 
  } 
}

// Demonstrate the ByTwos interface. 
 
public class SeriesDemo { 
  public static void Main() { 
    ByTwos ob = new ByTwos(); 
 
    for(int i=0; i < 5; i++) 
      Console.WriteLine("Next value is " + 
                         ob.getNext()); 
 
    Console.WriteLine("\nResetting"); 
    ob.reset(); 
    for(int i=0; i < 5; i++) 
      Console.WriteLine("Next value is " + 
                         ob.getNext()); 
 
    Console.WriteLine("\nStarting at 100"); 
    ob.setStart(100); 
    for(int i=0; i < 5; i++) 
      Console.WriteLine("Next value is " + 
                         ob.getNext()); 
  } 
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» Interface