C Primer Plus (5th Edition)

   

Listing 15.2 shows the most important code fragments for defining two namespaces called ElevatorSimulation (spanning lines 3 61 and containing the Elevator, Person, and Building classes) and BankSimulation (in lines 63 101 and containing the Account, Bank, and Building classes). The two namespaces are positioned beside each other in the same compilation unit. The class MyGlobalNamespaceClass (lines 103 120) has been thrown in to demonstrate a class that exists beside two namespaces and outside any namespace. The latter fact, as discussed in the previous section, causes it to belong to the global nameless namespace.

Listing 15.2 Two Namespaces Written Beside Each Other in a Compilation Unit

001: using System; 002: 003: namespace ElevatorSimulation 004: { 005: class Elevator 006: { ... 010: private Person passenger; ... 020: } ... 030: class Person 031: { ... 040: } ... 042: class Building 043: { ... 049: } 050: } 051: 052: namespace BankSimulation 053: { 054: using System.Collections; 055: class Account 056: { 057: private ElevatorSimulation.Person accountholder; ... 060: } 061: class Bank 062: { ... 070: public void PrintBankDetails() 071: { ... 075: Console.WriteLine("Bank name: ..."); 080: } 090: } 091: class Building 092: { ... 100: } 101: } 102: 103: class MyGlobalNamespaceClass 104: { ... 120: }

Note: The line numbers are somewhat arbitrary (but always increasing) because of the code fragments that are not shown. This code does not compile.

There are a few important points to notice about the source code in Listing 15.2:


   

Категории