Add a method to Building : Class Method : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » Class Method »

 

Add a method to Building









    

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// 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. 
public class BuildingDemo2 {   
  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(); 
  }   
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» Class Method