⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap10.lst

📁 Csharp2完全参考手册源代码 详细的说明可以在书里看到 该书是08年刚出炉很新鲜
💻 LST
📖 第 1 页 / 共 2 页
字号:
// Demonstrate a property. 
class PropertyDemo {  
  public static void Main() {  
    SimpProp ob = new SimpProp(); 
 
    Console.WriteLine("Original value of ob.MyProp: " + ob.MyProp); 
 
    ob.MyProp = 100; // assign value 
    Console.WriteLine("Value of ob.MyProp: " + ob.MyProp); 
 
    // Can't assign negative value to prop 
    Console.WriteLine("Attempting to assign -10 to ob.MyProp"); 
    ob.MyProp = -10; 
    Console.WriteLine("Value of ob.MyProp: " + ob.MyProp); 
  } 
}

listing 6
// Add Length property to FailSoftArray. 
 
using System; 
 
class FailSoftArray {  
  int[] a; // reference to underlying array  
  int len; // length of array -- underlies Length property 
 
  public bool errflag; // indicates outcome of last operation 
   
  // Construct array given its size.  
  public FailSoftArray(int size) { 
    a = new int[size]; 
    len = size;  
  } 
 
  // Read-only Length property. 
  public int Length { 
    get { 
      return len; 
    } 
  } 
 
  // This is the indexer for FailSoftArray. 
  public int this[int index] { 
    // This is the get accessor. 
    get { 
      if(ok(index)) { 
        errflag = false; 
        return a[index]; 
      } else { 
        errflag = true; 
        return 0; 
      } 
    } 
 
    // This is the set accessor 
    set { 
      if(ok(index)) { 
        a[index] = value; 
        errflag = false; 
      } 
      else errflag = true; 
    } 
  } 
 
  // Return true if index is within bounds. 
  private bool ok(int index) { 
   if(index >= 0 & index < Length) return true; 
   return false; 
  } 
}  
  
// Demonstrate the improved fail-soft array. 
class ImprovedFSDemo {  
  public static void Main() {  
    FailSoftArray fs = new FailSoftArray(5); 
    int x; 
 
    // can read Length 
    for(int i=0; i < fs.Length; i++) 
      fs[i] = i*10; 
 
    for(int i=0; i < fs.Length; i++) { 
      x = fs[i]; 
      if(x != -1) Console.Write(x + " "); 
    } 
    Console.WriteLine(); 
 
    // fs.Length = 10; // Error, illegal! 
  } 
}

listing 7
// Convert errflag into a property. 
 
using System; 
 
class FailSoftArray {  
  int[] a; // reference to underlying array  
  int len; // length of array 
 
  bool errflag; // now private 
   
  // Construct array given its size.  
  public FailSoftArray(int size) { 
    a = new int[size]; 
    len = size;  
  } 
 
  // Read-only Length property. 
  public int Length { 
    get { 
      return len; 
    } 
  } 
 
  // Read-only Error property. 
  public bool Error { 
    get { 
      return errflag; 
    } 
  } 
 
  // This is the indexer for FailSoftArray. 
  public int this[int index] { 
    // This is the get accessor. 
    get { 
      if(ok(index)) { 
        errflag = false; 
        return a[index]; 
      } else { 
        errflag = true; 
        return 0; 
      } 
    } 
 
    // This is the set accessor 
    set { 
      if(ok(index)) { 
        a[index] = value; 
        errflag = false; 
      } 
      else errflag = true; 
    } 
  } 
 
  // Return true if index is within bounds. 
  private bool ok(int index) { 
   if(index >= 0 & index < Length) return true; 
   return false; 
  } 
}  
  
// Demonstrate the improved fail-soft array. 
class FinalFSDemo {  
  public static void Main() {  
    FailSoftArray fs = new FailSoftArray(5); 
 
    // use Error property 
    for(int i=0; i < fs.Length + 1; i++) { 
      fs[i] = i*10; 
      if(fs.Error)  
        Console.WriteLine("Error with index " + i); 
    } 
 
  } 
}

listing 8
// Use an access modifier with an accessor. 
  
using System;  
  
class PropAccess {   
  int prop; // field being managed by MyProp  
  
  public PropAccess() { prop = 0; }  
  
  /* This is the property that supports access to  
     the private instance variable prop.  It allows 
     any code to obtain the value of prop, but 
     only other class members can set the value 
     of prop. */  
  public int MyProp {  
    get {  
      return prop;  
    }  
    private set { // now, private  
      prop = value;  
    }   
  }  
 
  // This class member increments the value of MyProp. 
  public void incrProp() { 
    MyProp++; // OK, in same class. 
  } 
}   
 
// Demonstrate accessor access modifier. 
class PropAccessDemo {   
  public static void Main() {   
    PropAccess ob = new PropAccess();  
  
    Console.WriteLine("Original value of ob.MyProp: " + ob.MyProp);  
  
//    ob.MyProp = 100; // can't access set 
  
    ob.incrProp(); 
    Console.WriteLine("Value of ob.MyProp after increment: " 
                      + ob.MyProp);  
  }  
}

listing 9
/* Create a specifiable range array class. 
   The RangeArray class allows indexing 
   to begin at some value other than zero. 
   When you create a RangeArray, you specify 
   the beginning and ending index. Negative 
   indexes are also  allowed.  For example, 
   you can create arrays that index from -5 to 5,  
   1 to 10, or 50 to 56. 
*/ 
  
using System;  
  
class RangeArray {   
  // private data 
  int[] a; // reference to underlying array   
  int lowerBound; // lowest index 
  int upperBound; // greatest index 
 
  // data for properties 
  int len; // underlying var for Length property 
  bool errflag; // underlying var for outcome  
    
  // Construct array given its size.  
  public RangeArray(int low, int high) {  
    high++; 
    if(high <= low) { 
      Console.WriteLine("Invalid Indices"); 
      high = 1; // create a minimal array for safety 
      low = 0; 
    } 
    a = new int[high - low];  
    len = high - low;   
 
    lowerBound = low; 
    upperBound = --high; 
  }  
  
  // Read-only Length property.  
  public int Length {  
    get {  
      return len;  
    }  
  }  
 
  // Read-only Error property.  
  public bool Error {  
    get {  
      return errflag;  
    }  
  }  
 
  // This is the indexer for RangeArray.  
  public int this[int index] {  
    // This is the get accessor.  
    get {  
      if(ok(index)) {  
        errflag = false;  
        return a[index - lowerBound];  
      } else {  
        errflag = true;  
        return 0;  
      }  
    }  
  
    // This is the set accessor  
    set {  
      if(ok(index)) {  
        a[index - lowerBound] = value;  
        errflag = false;  
      }  
      else errflag = true;  
    }  
  }  
  
  // Return true if index is within bounds.  
  private bool ok(int index) {  
    if(index >= lowerBound & index <= upperBound) return true;  
    return false;  
  }  
}   
   
// Demonstrate the index-range array.  
class RangeArrayDemo {   
  public static void Main() {   
    RangeArray ra = new RangeArray(-5, 5);  
    RangeArray ra2 = new RangeArray(1, 10);  
    RangeArray ra3 = new RangeArray(-20, -12);  
 
    // Demonstrate ra 
    Console.WriteLine("Length of ra: " + ra.Length); 
 
    for(int i = -5; i <= 5; i++) 
      ra[i] = i; 
   
    Console.Write("Contents of ra: "); 
    for(int i = -5; i <= 5; i++) 
      Console.Write(ra[i] + " "); 
 
    Console.WriteLine("\n"); 
 
    // Demonstrate ra2 
    Console.WriteLine("Length of ra2: " + ra2.Length); 
 
    for(int i = 1; i <= 10; i++) 
      ra2[i] = i; 
 
    Console.Write("Contents of ra2: "); 
    for(int i = 1; i <= 10; i++) 
      Console.Write(ra2[i] + " "); 
 
    Console.WriteLine("\n"); 
 
    // Demonstrate ra3 
    Console.WriteLine("Length of ra3: " + ra3.Length); 
 
    for(int i = -20; i <= -12; i++) 
      ra3[i] = i; 
 
    Console.Write("Contents of ra3: "); 
    for(int i = -20; i <= -12; i++) 
      Console.Write(ra3[i] + " "); 
 
    Console.WriteLine("\n"); 
 
  }  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -