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

📄 chap9.lst

📁 Csharp2完全参考手册源代码 详细的说明可以在书里看到 该书是08年刚出炉很新鲜
💻 LST
📖 第 1 页 / 共 2 页
字号:
 
    Console.WriteLine(); 
 
    Console.WriteLine("Control a loop using a ThreeD object."); 
    do { 
      b.show(); 
      b--; 
    } while(b); 
 
  }   
}

listing 7
// A simple way to overload !, |, and & for ThreeD. 
 
using System;   
   
// A three-dimensional coordinate class.   
class ThreeD {   
  int x, y, z; // 3-D coordinates     
   
  public ThreeD() { x = y = z = 0; }   
  public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }   
 
 
  // Overload |.   
  public static bool operator |(ThreeD op1, ThreeD op2)   
  {  
    if( ((op1.x != 0) || (op1.y != 0) || (op1.z != 0)) | 
       ((op2.x != 0) || (op2.y != 0) || (op2.z != 0)) ) 
      return true;   
    else   
      return false;   
  }   
 
  // Overload &.   
  public static bool operator &(ThreeD op1, ThreeD op2)   
  {   
    if( ((op1.x != 0) && (op1.y != 0) && (op1.z != 0)) & 
       ((op2.x != 0) && (op2.y != 0) && (op2.z != 0)) ) 
      return true;   
    else   
      return false;   
  }   
 
  // Overload !.   
  public static bool operator !(ThreeD op)   
  {   
    if((op.x != 0) || (op.y != 0) || (op.z != 0)) 
      return false; 
    else return true;   
  }   
 
  // Show X, Y, Z coordinates.   
  public void show()   
  {   
    Console.WriteLine(x + ", " + y + ", " + z);   
  }   
}   
   
class TrueFalseDemo {   
  public static void Main() {   
    ThreeD a = new ThreeD(5, 6, 7);   
    ThreeD b = new ThreeD(10, 10, 10);   
    ThreeD c = new ThreeD(0, 0, 0);   
   
    Console.Write("Here is a: ");   
    a.show();   
    Console.Write("Here is b: ");   
    b.show();   
    Console.Write("Here is c: ");   
    c.show();   
    Console.WriteLine();   
 
    if(!a) Console.WriteLine("a is false."); 
    if(!b) Console.WriteLine("b is false."); 
    if(!c) Console.WriteLine("c is false."); 
 
    Console.WriteLine(); 
 
    if(a & b) Console.WriteLine("a & b is true."); 
    else Console.WriteLine("a & b is false."); 
 
    if(a & c) Console.WriteLine("a & c is true."); 
    else Console.WriteLine("a & c is false."); 
 
    if(a | b) Console.WriteLine("a | b is true."); 
    else Console.WriteLine("a | b is false."); 
 
    if(a | c) Console.WriteLine("a | c is true."); 
    else Console.WriteLine("a | c is false."); 
  }   
}

listing 8
/* A better way to overload !, |, and & for ThreeD. 
   This version automatically enables the && and || operators. */ 
 
using System;   
   
// A three-dimensional coordinate class.   
class ThreeD {   
  int x, y, z; // 3-D coordinates     
   
  public ThreeD() { x = y = z = 0; }   
  public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }   
 
 
  // Overload | for short-circuit evaluation.   
  public static ThreeD operator |(ThreeD op1, ThreeD op2)   
  {  
    if( ((op1.x != 0) || (op1.y != 0) || (op1.z != 0)) | 
       ((op2.x != 0) || (op2.y != 0) || (op2.z != 0)) ) 
      return new ThreeD(1, 1, 1);   
    else   
      return new ThreeD(0, 0, 0);   
  }   
 
  // Overload & for short-circuit evaluation.   
  public static ThreeD operator &(ThreeD op1, ThreeD op2)   
  {   
    if( ((op1.x != 0) && (op1.y != 0) && (op1.z != 0)) & 
       ((op2.x != 0) && (op2.y != 0) && (op2.z != 0)) ) 
      return new ThreeD(1, 1, 1);   
    else   
      return new ThreeD(0, 0, 0);   
  }   
 
  // Overload !.   
  public static bool operator !(ThreeD op)   
  {   
    if(op) return false;   
    else return true;   
  }   
 
  // Overload true.   
  public static bool operator true(ThreeD op) { 
    if((op.x != 0) || (op.y != 0) || (op.z != 0)) 
      return true; // at least one coordinate is non-zero 
    else 
      return false; 
  }   
 
  // Overload false. 
  public static bool operator false(ThreeD op) { 
    if((op.x == 0) && (op.y == 0) && (op.z == 0)) 
      return true; // all coordinates are zero 
    else 
      return false; 
  }   
 
  // Show X, Y, Z coordinates.   
  public void show()   
  {   
    Console.WriteLine(x + ", " + y + ", " + z);   
  }   
}   
   
class TrueFalseDemo {   
  public static void Main() {   
    ThreeD a = new ThreeD(5, 6, 7);   
    ThreeD b = new ThreeD(10, 10, 10);   
    ThreeD c = new ThreeD(0, 0, 0);   
   
    Console.Write("Here is a: ");   
    a.show();   
    Console.Write("Here is b: ");   
    b.show();   
    Console.Write("Here is c: ");   
    c.show();   
    Console.WriteLine();   
   
    if(a) Console.WriteLine("a is true."); 
    if(b) Console.WriteLine("b is true."); 
    if(c) Console.WriteLine("c is true."); 
 
    if(!a) Console.WriteLine("a is false."); 
    if(!b) Console.WriteLine("b is false."); 
    if(!c) Console.WriteLine("c is false."); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("Use & and |"); 
    if(a & b) Console.WriteLine("a & b is true."); 
    else Console.WriteLine("a & b is false."); 
 
    if(a & c) Console.WriteLine("a & c is true."); 
    else Console.WriteLine("a & c is false."); 
 
    if(a | b) Console.WriteLine("a | b is true."); 
    else Console.WriteLine("a | b is false."); 
 
    if(a | c) Console.WriteLine("a | c is true."); 
    else Console.WriteLine("a | c is false."); 
 
    Console.WriteLine(); 
 
    // now use short-circuit ops 
    Console.WriteLine("Use short-circuit && and ||"); 
    if(a && b) Console.WriteLine("a && b is true."); 
    else Console.WriteLine("a && b is false."); 
 
    if(a && c) Console.WriteLine("a && c is true."); 
    else Console.WriteLine("a && c is false."); 
 
    if(a || b) Console.WriteLine("a || b is true."); 
    else Console.WriteLine("a || b is false."); 
 
    if(a || c) Console.WriteLine("a || c is true."); 
    else Console.WriteLine("a || c is false."); 
  }   
}

listing 9
// An example that uses an implicit conversion operator. 
 
using System; 
 
// A three-dimensional coordinate class. 
class ThreeD { 
  int x, y, z; // 3-D coordinates   
 
  public ThreeD() { x = y = z = 0; } 
  public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } 
 
  // Overload binary +. 
  public static ThreeD operator +(ThreeD op1, ThreeD op2) 
  { 
    ThreeD result = new ThreeD(); 
 
    result.x = op1.x + op2.x;  
    result.y = op1.y + op2.y;  
    result.z = op1.z + op2.z;  
 
    return result; 
  } 
 
  // An implicit conversion from ThreeD to int. 
  public static implicit operator int(ThreeD op1) 
  { 
    return op1.x * op1.y * op1.z; 
  } 
   
  // Show X, Y, Z coordinates. 
  public void show() 
  { 
    Console.WriteLine(x + ", " + y + ", " + z); 
  } 
} 
 
class ThreeDDemo { 
  public static void Main() { 
    ThreeD a = new ThreeD(1, 2, 3); 
    ThreeD b = new ThreeD(10, 10, 10); 
    ThreeD c = new ThreeD(); 
    int i; 
 
    Console.Write("Here is a: "); 
    a.show(); 
    Console.WriteLine(); 
    Console.Write("Here is b: "); 
    b.show(); 
    Console.WriteLine(); 
 
    c = a + b; // add a and b together 
    Console.Write("Result of a + b: "); 
    c.show(); 
    Console.WriteLine(); 
 
    i = a; // convert to int 
    Console.WriteLine("Result of i = a: " + i); 
    Console.WriteLine(); 
 
    i = a * 2 - b; // convert to int 
    Console.WriteLine("result of a * 2 - b: " + i); 
  } 
}

listing 10
// Use an explicit conversion. 
 
using System; 
 
// A three-dimensional coordinate class. 
class ThreeD { 
  int x, y, z; // 3-D coordinates   
 
  public ThreeD() { x = y = z = 0; } 
  public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } 
 
  // Overload binary +. 
  public static ThreeD operator +(ThreeD op1, ThreeD op2) 
  { 
    ThreeD result = new ThreeD(); 
 
    result.x = op1.x + op2.x;  
    result.y = op1.y + op2.y;  
    result.z = op1.z + op2.z;  
 
    return result; 
  } 
 
  // This is now explicit. 
  public static explicit operator int(ThreeD op1) 
  { 
    return op1.x * op1.y * op1.z; 
  } 
   
  // Show X, Y, Z coordinates. 
  public void show() 
  { 
    Console.WriteLine(x + ", " + y + ", " + z); 
  } 
} 
 
class ThreeDDemo { 
  public static void Main() { 
    ThreeD a = new ThreeD(1, 2, 3); 
    ThreeD b = new ThreeD(10, 10, 10); 
    ThreeD c = new ThreeD(); 
    int i; 
 
    Console.Write("Here is a: "); 
    a.show(); 
    Console.WriteLine(); 
    Console.Write("Here is b: "); 
    b.show(); 
    Console.WriteLine(); 
 
    c = a + b; // add a and b together 
    Console.Write("Result of a + b: "); 
    c.show(); 
    Console.WriteLine(); 
 
    i = (int) a; // explicitly convert to int -- cast required 
    Console.WriteLine("Result of i = a: " + i); 
    Console.WriteLine(); 
 
    i = (int)a * 2 - (int)b; // casts required 
    Console.WriteLine("result of a * 2 - b: " + i); 
 
  } 
}

listing 11
// Create a 4-bit type called Nybble. 
 
using System;  
  
// A 4-bit type. 
class Nybble {  
  int val; // underlying storage 
 
  public Nybble() { val = 0; }  
 
  public Nybble(int i) { 
    val = i; 
    val = val & 0xF; // retain lower 4 bits 
  } 
  
  // Overload binary + for Nybble + Nybble.  
  public static Nybble operator +(Nybble op1, Nybble op2)  
  {  
    Nybble result = new Nybble();  
  
    result.val = op1.val + op2.val;  
 
    result.val = result.val & 0xF; // retain lower 4 bits  
  
    return result;  
  }  
  
  // Overload binary + for Nybble + int.  
  public static Nybble operator +(Nybble op1, int op2)  
  {  
    Nybble result = new Nybble();  
  
    result.val = op1.val + op2;  
 
    result.val = result.val & 0xF; // retain lower 4 bits  
  
    return result;  
  }  
  
  // Overload binary + for int + Nybble.  
  public static Nybble operator +(int op1, Nybble op2)  
  {  
    Nybble result = new Nybble();  
  
    result.val = op1 + op2.val;  
 
    result.val = result.val & 0xF; // retain lower 4 bits  
  
    return result;  
  }  
  
  // Overload ++. 
  public static Nybble operator ++(Nybble op) 
  { 
    op.val++; 
 
    op.val = op.val & 0xF; // retain lower 4 bits 
 
    return op; 
  } 
 
  // Overload >. 
  public static bool operator >(Nybble op1, Nybble op2) 
  { 
    if(op1.val > op2.val) return true; 
    else return false; 
  } 
 
  // Overload <. 
  public static bool operator <(Nybble op1, Nybble op2) 
  { 
    if(op1.val < op2.val) return true; 
    else return false; 
  } 
 
  // Convert a Nybble into an int. 
  public static implicit operator int (Nybble op) 
  { 
    return op.val; 
  } 
 
  // Convert an int into a Nybble. 
  public static implicit operator Nybble (int op) 
  { 
    return new Nybble(op); 
  } 
}  
  
class NybbleDemo {  
  public static void Main() {  
    Nybble a = new Nybble(1);  
    Nybble b = new Nybble(10);  
    Nybble c = new Nybble();  
    int t; 
  
    Console.WriteLine("a: " + (int) a); 
    Console.WriteLine("b: " + (int) b); 
 
    // use a Nybble in an if statement 
    if(a < b) Console.WriteLine("a is less than b\n"); 
 
    // Add two Nybbles together 
    c = a + b; 
    Console.WriteLine("c after c = a + b: " + (int) c); 
 
    // Add an int to a Nybble 
    a += 5; 
    Console.WriteLine("a after a += 5: " + (int) a); 
 
    Console.WriteLine(); 
 
    // use a Nybble in an int expression 
    t = a * 2 + 3; 
    Console.WriteLine("Result of a * 2 + 3: " + t); 
     
    Console.WriteLine(); 
 
    // illustrate int assignment and overflow 
    a = 19; 
    Console.WriteLine("Result of a = 19: " + (int) a); 
     
    Console.WriteLine(); 
 
    // use a Nybble to control a loop     
    Console.WriteLine("Control a for loop with a Nybble."); 
    for(a = 0; a < 10; a++) 
      Console.Write((int) a + " "); 
 
    Console.WriteLine(); 
  } 
}

⌨️ 快捷键说明

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