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

📄 computer.java

📁 北京大学出版社的
💻 JAVA
字号:
package examples.inner;

/** Class to represent the memory and hard drive
  * information of a computer
  */
public class Computer {

   /** Maximum number of hard drives in a computer */
   public static final int MAX_DRIVES = 4;
   private int installedDrives = 0;
   private HardDrive[] drives
      = new HardDrive[MAX_DRIVES];
   private int memMegs;

   /** Test method
     * @param args not used
     * @exception Exception if a failure occurs while
     *    adding a HardDrive object
     */
   public static void main( String[] args )
                            throws Exception {
      Computer atWork = new Computer( 64 );
      System.out.println( atWork );
      // must specify the enclosing object here
      HardDrive IDE1 = atWork.new HardDrive( 1024 );
      HardDrive IDE2 = atWork.new HardDrive( 2048 );
      System.out.println( atWork );
   }
   /** Construct a Computer object
     * @param memSize the amount of memory in MB
     */
   public Computer( int memSize ) {
      memMegs = memSize;
   }

   /** Provide a string representing the computer
     * @return string representation of the object
     */
   public String toString() {
      StringBuffer sb
         = new StringBuffer( "Memory: " + memMegs
                             + "MB" );
      for ( int i=0; i<installedDrives; i++ ) {
         sb.append( ", Drive" + i + ": " );
         sb.append( drives[i].size + "MB" );
      }
      return sb.toString();
   }

   /** Class representing a hard drive within
     * a computer
     */
   public class HardDrive {

      private int size;

      /** Construct a hard drive object and add it
        * to the list of installed drives
        * if there is room
        * @param size Size of the drive in MB
        * @exception Exception thrown
        *    if there isn't room for
        *    the hard drive being added
        */
      public HardDrive( int size ) throws Exception {
         this.size = size;
         // add this drive to the enclosing computer
         if ( installedDrives < MAX_DRIVES ) {
            drives[installedDrives++] = this;
         } else {
            throw new Exception( "Sorry, no "
                                 + "more room." );
         }
      }
   }
}

⌨️ 快捷键说明

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