日期:2014-05-20  浏览次数:20782 次

java同一文件不能有两个类?
《重构》中的开始的代码如下:
/*
 * Main.java
 *
 * Created on 2008年6月5日, 下午6:25
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package statement;

import java.util.Enumeration;
import java.util.Vector;

/**
 *
 * @author Administrator
 */
public class Main {
   
  /** Creates a new instance of Main */
  public Main() {
  }
   
  /**
  * @param args the command line arguments
  */
  public static void main(String[] args) {
  // TODO code application logic here
  }
   
}

public class Movie
{
  public static final int CHILDRENS = 2;
  public static final int REGULAR = 0;
  public static final int NEW_RELEASE = 1;
  private String _title; //名称
  private int _priceCode; //价格(代号)
  public Movie(String title, int priceCode)
  {
  _title = title;
  _priceCode = priceCode;
  }
  public int getPriceCode()
  {
  return _priceCode;
  }
  public void setPriceCode(int arg)
  {
  _priceCode = arg;
  }
  public String getTitle()
  {
  return _title;
  }
}
class Rental
{
  private Movie _movie;
  private int _daysRented;
  public Rental(Movie movie,int daysRented)
  {
  _movie = movie;
  _daysRented = daysRented;
  }
  public int getDaysrented()
  {
  return _daysRented;
  }
  public Movie getMovie()
  {
  return _movie;
  }
}

class Customer
{
  private String _name; //姓名
  private Vector _rentals = new Vector(); //租赁记录
  public Customer(String name)
  {
  _name = name;
  }
  public void addRental(Rental arg)
  {
  _rentals.addElement(arg);
  }
  public String getName()
  {
  return _name;
  }
  public String statement()
  {
  double totalAmount = 0; //总消费金额
  int frequentRenterPoints = 0;
  Enumeration rentals = _rentals.elements();
  String result = "Rental Record for" + getName() + "\n";
  while(rentals.hasMoreElements())
  {
  double thisAmount = 0;
  Rental each = (Rental) rentals.nextElement();
  //determine amounts ofr each line 
  switch(each.getMovie().getPriceCode())
  {
  case Movie.REGULAR: //普通片
  thisAmount += 2;
  if(each.getDaysrented()>2)
  thisAmount += (each.getDaysrented()-2)*1.5;
  break;
  case Movie.NEW_RELEASE: //新片
  thisAmount += each.getDaysrented()*3;
  break;
  case Movie.CHILDRENS: //儿童片
  thisAmount += 1.5;
  if(each.getDaysrented()>3)
  thisAmount += (each.getDaysrented()-3)*1.5;
  break;
  }
  //add frequent renter points (累加 常客积点)