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

📄 chap6.lst

📁 Csharp2完全参考手册源代码 详细的说明可以在书里看到 该书是08年刚出炉很新鲜
💻 LST
字号:
listing 1
// A program that uses the Building class.   
 
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
}   
   
// This class declares an object of type Building.   
class BuildingDemo {   
  public static void Main() {   
    Building house = new Building(); // create a Building object 
    int areaPP; // area per person 
   
    // assign values to fields in house 
    house.occupants = 4;  
    house.area = 2500;  
    house.floors = 2;  
   
    // compute the area per person 
    areaPP = house.area / house.occupants;  
   
    Console.WriteLine("house has:\n  " + 
                      house.floors + " floors\n  " + 
                      house.occupants + " occupants\n  " + 
                      house.area + " total area\n  " + 
                      areaPP + " area per person"); 
  }   
}

listing 2
// This program creates two Building objects. 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
}   
   
// This class declares two objects of type Building.   
class BuildingDemo {   
  public static void Main() {   
    Building house = new Building();   
    Building office = new Building(); 
 
    int areaPP; // area per person 
   
    // assign values to fields in house 
    house.occupants = 4;  
    house.area = 2500;  
    house.floors = 2;  
 
    // assign values to fields in office 
    office.occupants = 25;  
    office.area = 4200;  
    office.floors = 3;  
   
    // compute the area per person in house 
    areaPP = house.area / house.occupants;  
   
    Console.WriteLine("house has:\n  " + 
                      house.floors + " floors\n  " + 
                      house.occupants + " occupants\n  " + 
                      house.area + " total area\n  " + 
                      areaPP + " area per person"); 
 
    Console.WriteLine(); 
 
    // compute the area per person in office 
    areaPP = office.area / office.occupants;  
 
    Console.WriteLine("office has:\n  " + 
                      office.floors + " floors\n  " + 
                      office.occupants + " occupants\n  " + 
                      office.area + " total area\n  " + 
                      areaPP + " area per person"); 
  }   
}

listing 3
// Add a method to Building. 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
 
  // Display the area per person.  
  public void areaPerPerson() {  
    Console.WriteLine("  " + area / occupants + 
                      " area per person"); 
  }  
}   
 
// Use the areaPerPerson() method. 
class BuildingDemo {   
  public static void Main() {   
    Building house = new Building();   
    Building office = new Building(); 
 
 
    // assign values to fields in house 
    house.occupants = 4;  
    house.area = 2500;  
    house.floors = 2;  
 
    // assign values to fields in office 
    office.occupants = 25;  
    office.area = 4200;  
    office.floors = 3;  
   
 
    Console.WriteLine("house has:\n  " + 
                      house.floors + " floors\n  " + 
                      house.occupants + " occupants\n  " + 
                      house.area + " total area"); 
    house.areaPerPerson(); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("office has:\n  " + 
                      office.floors + " floors\n  " + 
                      office.occupants + " occupants\n  " + 
                      office.area + " total area"); 
    office.areaPerPerson(); 
  }   
}

listing 4
// Return a value from areaPerPerson(). 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
 
  // Return the area per person.  
  public int areaPerPerson() {  
    return area / occupants; 
  }  
}   
   
// Use the return value from areaPerPerson().   
class BuildingDemo {   
  public static void Main() {   
    Building house = new Building();   
    Building office = new Building(); 
    int areaPP; // area per person 
 
    // assign values to fields in house 
    house.occupants = 4;  
    house.area = 2500;  
    house.floors = 2;  
 
    // assign values to fields in office 
    office.occupants = 25;  
    office.area = 4200;  
    office.floors = 3;  
   
    // obtain area per person for house 
    areaPP = house.areaPerPerson(); 
 
    Console.WriteLine("house has:\n  " + 
                      house.floors + " floors\n  " + 
                      house.occupants + " occupants\n  " + 
                      house.area + " total area\n  " + 
                      areaPP + " area per person"); 
 
 
    Console.WriteLine(); 
 
    // obtain area per person for office 
    areaPP = office.areaPerPerson(); 
 
    Console.WriteLine("office has:\n  " + 
                      office.floors + " floors\n  " + 
                      office.occupants + " occupants\n  " + 
                      office.area + " total area\n  " + 
                      areaPP + " area per person"); 
  }   
}

listing 5
// A simple example that uses a parameter. 
 
using System; 
 
class ChkNum {  
  // Return true if x is prime. 
  public bool isPrime(int x) { 
    for(int i=2; i <= x/i; i++) 
      if((x %i) == 0) return false; 
 
    return true; 
  } 
}  
  
class ParmDemo {  
  public static void Main() {  
    ChkNum ob = new ChkNum(); 
 
    for(int i=1; i < 10; i++) 
      if(ob.isPrime(i)) Console.WriteLine(i + " is prime."); 
      else Console.WriteLine(i + " is not prime."); 
 
  }  
}

listing 6
// Add a method that takes two arguments. 
 
using System; 
 
class ChkNum {  
  // Return true if x is prime. 
  public bool isPrime(int x) { 
    for(int i=2; i <= x/i; i++) 
      if((x %i) == 0) return false; 
 
    return true; 
  } 
 
  // Return the least common factor. 
  public int leastComFactor(int a, int b) { 
    int max; 
 
    if(isPrime(a) | isPrime(b)) return 1; 
 
    max = a < b ? a : b; 
 
    for(int i=2; i <= max/2; i++) 
      if(((a%i) == 0) & ((b%i) == 0)) return i; 
 
    return 1; 
  } 
}  
  
class ParmDemo {  
  public static void Main() {  
    ChkNum ob = new ChkNum(); 
    int a, b; 
 
    for(int i=1; i < 10; i++) 
      if(ob.isPrime(i)) Console.WriteLine(i + " is prime."); 
      else Console.WriteLine(i + " is not prime."); 
 
    a = 7; 
    b = 8; 
    Console.WriteLine("Least common factor for " + 
                      a + " and " + b + " is " + 
                      ob.leastComFactor(a, b)); 
 
    a = 100; 
    b = 8; 
    Console.WriteLine("Least common factor for " + 
                      a + " and " + b + " is " + 
                      ob.leastComFactor(a, b)); 
 
    a = 100; 
    b = 75; 
    Console.WriteLine("Least common factor for " + 
                      a + " and " + b + " is " + 
                      ob.leastComFactor(a, b)); 
 
  }  
}

listing 7
/* 
   Add a parameterized method that computes the   
   maximum number of people that can occupy a 
   building assuming each needs a specified 
   minimum space. 
*/ 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
 
  // Return the area per person.  
  public int areaPerPerson() {  
    return area / occupants; 
  }  
 
  /* Return the maximum number of occupants if each 
     is to have at least the specified minimum area. */ 
  public int maxOccupant(int minArea) { 
    return area / minArea; 
  } 
}   
 
// Use maxOccupant(). 
class BuildingDemo {   
  public static void Main() {   
    Building house = new Building();   
    Building office = new Building(); 
 
    // assign values to fields in house 
    house.occupants = 4;  
    house.area = 2500;  
    house.floors = 2;  
 
    // assign values to fields in office 
    office.occupants = 25;  
    office.area = 4200;  
    office.floors = 3;  
   
    Console.WriteLine("Maximum occupants for house if each has " + 
                      300 + " square feet: " + 
                      house.maxOccupant(300)); 
 
    Console.WriteLine("Maximum occupants for office if each has " + 
                      300 + " square feet: " + 
                      office.maxOccupant(300)); 
  }   
}

listing 8
public void m() { 
  char a, b; 
 
  // ... 
 
  if(a==b) { 
    Console.WriteLine("equal"); 
    return; 
  } else { 
    Console.WriteLine("not equal"); 
    return; 
  } 
  Console.WriteLine("this is unreachable"); 
}

listing 9
// A simple constructor. 
 
using System; 
 
class MyClass { 
  public int x; 
 
  public MyClass() { 
    x = 10; 
  }   
}   
   
class ConsDemo {   
  public static void Main() {   
    MyClass t1 = new MyClass(); 
    MyClass t2 = new MyClass(); 
 
    Console.WriteLine(t1.x + " " + t2.x); 
  }   
}

listing 10
// A parameterized constructor. 
 
using System; 
 
class MyClass { 
  public int x; 
 
  public MyClass(int i) { 
    x = i; 
  }   
}   
   
class ParmConsDemo {   
  public static void Main() {   
    MyClass t1 = new MyClass(10); 
    MyClass t2 = new MyClass(88); 
 
    Console.WriteLine(t1.x + " " + t2.x); 
  }   
}

listing 11
// Add a constructor to Building. 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
 
 
  public Building(int f, int a, int o) { 
    floors = f; 
    area = a; 
    occupants = o; 
  } 
 
  // Display the area per person.  
  public int areaPerPerson() {  
    return area / occupants; 
  }  
 
  /* Return the maximum number of occupants if each 
     is to have at least the specified minimum area. */ 
  public int maxOccupant(int minArea) { 
    return area / minArea; 
  } 
}   
   
// Use the parameterized Building constructor. 
class BuildingDemo {   
  public static void Main() {   
    Building house = new Building(2, 2500, 4);   
    Building office = new Building(3, 4200, 25); 
 
    Console.WriteLine("Maximum occupants for house if each has " + 
                      300 + " square feet: " + 
                      house.maxOccupant(300)); 
 
    Console.WriteLine("Maximum occupants for office if each has " + 
                      300 + " square feet: " + 
                      office.maxOccupant(300)); 
  }   
}

listing 12
// Use new with a value type. 
 
using System; 
 
class newValue {  
  public static void Main() {  
    int i = new int(); // initialize i to zero 
 
    Console.WriteLine("The value of i is: " + i); 
  }  
}

listing 13
// Demonstrate a destructor. 
 
using System; 
 
class Destruct {  
  public int x;  
  
  public Destruct(int i) {  
    x = i;  
  }    
 
  // called when object is recycled 
  ~Destruct() { 
    Console.WriteLine("Destructing " + x); 
  } 
   
  // generates an object that is immediately destroyed 
  public void generator(int i) { 
    Destruct o = new Destruct(i); 
  } 
 
}    
    
class DestructDemo {    
  public static void Main() {    
    int count; 
 
    Destruct ob = new Destruct(0); 
 
    /* Now, generate a large number of objects.  At 
       some point, garbage collection will occur.  
       Note: you might need to increase the number 
       of objects generated in order to force 
       garbage collection. */ 
 
    for(count=1; count < 100000; count++) 
      ob.generator(count);  
 
    Console.WriteLine("Done"); 
  }    
}

listing 14
using System; 
 
class Rect { 
  public int width; 
  public int height; 
 
  public Rect(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  public int area() { 
    return width * height; 
  } 
} 
  
class UseRect { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
    Rect r2 = new Rect(7, 9); 
 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine("Area of r2: " + r2.area()); 
 
  } 
}

listing 15
using System; 
 
class Rect { 
  public int width; 
  public int height; 
 
  public Rect(int w, int h) { 
    this.width = w; 
    this.height = h; 
  } 
 
  public int area() { 
    return this.width * this.height; 
  } 
} 
  
class UseRect { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
    Rect r2 = new Rect(7, 9); 
 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine("Area of r2: " + r2.area()); 
 
  } 
}

⌨️ 快捷键说明

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