首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java同一文件不能有两个类?解决方案

2012-03-16 
java同一文件不能有两个类?《重构》中的开始的代码如下:/** Main.java** Created on 2008年6月5日, 下午6:25

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 (累加 常客积点)


  frequentRenterPoints++;
  //add bonus for a two day new release rental 
  if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && 
  each.getDaysrented() >1)
  frequentRenterPoints ++;
  result += "\t"+each.getMovie().getTitle()+"\t" +
  String.valueOf(thisAmount) + "\n";
  totalAmount += thisAmount;
  }
  //add footer lines (结尾打印)
  result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
  result += "You earned" + String.valueOf(frequentRenterPoints) + 
  "frequent renter points \n";
  return result;
  }
}

用NetBeans编译,为什么提示说 movie class应在文件中声明?
如何改正,还有这个没有主函数的程序能运行吗?

[解决办法]
如何改正,还有这个没有主函数的程序能运行吗?
------------------------

Main class里面不是有主函数吗,在里面写 

Customer c=new Customer("123");
c.***

不就运行起来了。

 



[解决办法]
就把
public class Movie
改掉就可以了,你的两个问题是一个嘛,
没有主函数当然不能运行.

热点排行