我无法将多个类放入一个文件中。例如,当我的文件看起来像:
public class FirstClass() {}
public class SecondClass() {}
public class ThirdClass() {}
我在编译过程中遇到错误。我不太确定是什么原因造成的。有任何想法吗?
一个 Java 文件可以包含多个类,但只有一个类可以是公共的。一旦public
从类中删除关键字,就可以将它们组合成一个 Java 文件。
冒着喂勺的风险
请阅读http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TheaterWindow extends JFrame
{
private JPanel pnlAdultTicketPrice, pnlAdultTicketsSold, pnlChildTicketPrice, pnlChildTicketsSold,
pnlCalculate, pnlMain;
private JLabel lblAdultTicketPrice, lblAdultTicketsSold, lblChildTicketPrice, lblChildTicketsSold;
private JTextField txtAdultTicketPrice, txtAdultTicketsSold, txtChildTicketPrice, txtChildTicketsSold;
private JButton btnCalculate;
public TheaterWindow()
{
// Sets window title
setTitle("Theater");
// Sets layout to BorderLayout
setLayout(new GridLayout(5,1));
// Specifies what happens when close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Builds the panels
buildPanels();
// Add the panels to the frame's content pane
add(pnlAdultTicketPrice);
add(pnlChildTicketPrice);
add(pnlAdultTicketsSold);
add(pnlChildTicketsSold);
add(pnlCalculate);
// Size the frame to fit all of the panels
pack();
// Display the window
setVisible(true);
}
private void buildPanels()
{
// Creates labels to display instructions
lblAdultTicketPrice = new JLabel("Adult ticket price");
lblChildTicketPrice = new JLabel("Child ticket price");
lblAdultTicketsSold = new JLabel("Adult tickets sold");
lblChildTicketsSold = new JLabel("Child tickets sold");
// Creates text fields that are 10 characters wide
txtAdultTicketPrice = new JTextField(10);
txtChildTicketPrice = new JTextField(10);
txtAdultTicketsSold = new JTextField(10);
txtChildTicketsSold = new JTextField(10);
// Creates button with caption
btnCalculate = new JButton("Calculate");
// Adds action listener to button
btnCalculate.addActionListener(new CalcButtonListener());
// Creates panels
pnlAdultTicketPrice = new JPanel();
pnlChildTicketPrice = new JPanel();
pnlAdultTicketsSold = new JPanel();
pnlChildTicketsSold = new JPanel();
pnlCalculate = new JPanel();
pnlMain = new JPanel();
// Adds elements to their proper panels
pnlAdultTicketPrice.add(lblAdultTicketPrice);
pnlAdultTicketPrice.add(txtAdultTicketPrice);
pnlChildTicketPrice.add(lblChildTicketPrice);
pnlChildTicketPrice.add(txtChildTicketPrice);
pnlAdultTicketsSold.add(lblAdultTicketsSold);
pnlAdultTicketsSold.add(txtAdultTicketsSold);
pnlChildTicketsSold.add(lblChildTicketsSold);
pnlChildTicketsSold.add(txtChildTicketsSold);
pnlCalculate.add(btnCalculate);
// Adds all of the above panels to a main panel
pnlMain.add(pnlAdultTicketPrice);
pnlMain.add(pnlChildTicketPrice);
pnlMain.add(pnlAdultTicketsSold);
pnlMain.add(pnlChildTicketsSold);
pnlMain.add(pnlCalculate);
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Creates object of Theater
Theater theater = new Theater();
// Sets the member variables of Theater to the user's input
theater.setAdultTicketPrice(Double.parseDouble(txtAdultTicketPrice.getText()));
theater.setChildTicketPrice(Double.parseDouble(txtChildTicketPrice.getText()));
theater.setAdultTicketsSold(Integer.parseInt(txtAdultTicketsSold.getText()));
theater.setChildTicketsSold(Integer.parseInt(txtChildTicketsSold.getText()));
// Creates DecimalFormat object for rounding
DecimalFormat dollar = new DecimalFormat("#.##");
// Display the charges.
JOptionPane.showMessageDialog(null, "Adult ticket gross: $" +
Double.valueOf(dollar.format(theater.getAdultGross())) + "\n" +
"Child ticket gross: $" + Double.valueOf(dollar.format(theater.getChildGross())) + "\n" +
"Adult ticket net: $" + Double.valueOf(dollar.format(theater.getAdultNet())) + "\n" +
"Child ticket net: $" + Double.valueOf(dollar.format(theater.getChildNet())) + "\n" +
"Total gross: $" + Double.valueOf(dollar.format(theater.getChildGross())) + "\n" +
"Total net: $" + Double.valueOf(dollar.format(theater.getTotalNet())));
}
}
public class Theater
{
private double PERCENTAGE_KEPT = 0.20;
private double adultTicketPrice, childTicketPrice;
private int adultTicketsSold, childTicketsSold;
public double getAdultGross()
{
return getAdultTicketPrice() * getAdultTicketsSold();
}
public double getAdultNet()
{
return PERCENTAGE_KEPT * getAdultGross();
}
public double getAdultTicketPrice()
{
return adultTicketPrice;
}
public int getAdultTicketsSold()
{
return adultTicketsSold;
}
public double getChildGross()
{
return getChildTicketPrice() * getChildTicketsSold();
}
public double getChildNet()
{
return PERCENTAGE_KEPT * getChildGross();
}
public double getChildTicketPrice()
{
return childTicketPrice;
}
public int getChildTicketsSold()
{
return childTicketsSold;
}
public double getTotalGross()
{
return getChildGross() + getAdultGross();
}
public double getTotalNet()
{
return getChildGross() + getChildNet();
}
public void setAdultTicketPrice(double adultTicketPrice)
{
this.adultTicketPrice = adultTicketPrice;
}
public void setAdultTicketsSold(int adultTicketsSold)
{
this.adultTicketsSold = adultTicketsSold;
}
public void setChildTicketPrice(double childTicketPrice)
{
this.childTicketPrice = childTicketPrice;
}
public void setChildTicketsSold(int childTicketsSold)
{
this.childTicketsSold = childTicketsSold;
}
}
}
是的,您可以将所有类写在一个 .java 文件中,但您必须只有一个公共类(如果文件名和类名相同)
例子:
甲类 { }
乙类 { }
C类{}
我假设你是非常初学者!只需将所有这些内容复制粘贴到一个文件中即可TheaterDemo.java
。并且不要忘记删除public
类声明开头的所有关键字。
一个 Java 文件最多可以包含一个顶级公共类。该公共顶级类可以包含任意数量的公共嵌套类。
您可以通过以下任何一种方法消除编译器错误:
嵌套名称不是文件基名的类。例如:
public class FirstClass() {
public class SecondClass() {}
public class ThirdClass() {}
}
除了名称为文件基名的一个类之外,从所有类中删除public
限定符。在 Java v1.1 中引入嵌套类之后,这种方法变得不那么普遍了。例如,在文件 FirstClass.java 中,您可以:
public class FirstClass() {}
class SecondClass() {}
class ThirdClass() {}
来自 Java 语言规范,第 7.6 节:顶级类型声明:
当且仅当包存储在文件系统中(第 7.2 节)时,如果在由类型名称组成的名称下的文件中未找到类型,则主机系统可能会选择强制执行它是编译时错误的限制如果满足以下任一条件,则加上扩展名(例如 .java 或 .jav):
该类型由声明该类型的包的其他编译单元中的代码引用。
该类型被声明为公共的(因此可以从其他包中的代码中访问)。
只需从所有其他类定义中删除 public 并将代码粘贴到TheaterDemo.java
文件中
public class TheaterDemo
{
public static void main(String[] args)
{
TheaterWindow theaterWindow = new TheaterWindow();
}
}
//Here class code after removing public
// Here another class code
我看到你已经完成了这种实现。请参考
private class CalcButtonListener implements ActionListener
在你的TheaterWindow
课上。
通过这样做,您正在创建内部类,即CalcButtonListener
是类的内部TheaterWindow
类。您可以将一些概念扩展到其他类。
java文件中类文件的个数没有限制。
但是每个源代码文件不能有多个公共类。此外,文件的名称必须与公共类的名称匹配。类似地,声明为 public class Dog { } 的类必须位于名为 Dog.java 的源代码文件中。
没有公共类的文件的名称可能与文件中的任何类都不匹配。