9

我无法将多个类放入一个文件中。例如,当我的文件看起来像:

public class FirstClass() {}
public class SecondClass() {}
public class ThirdClass() {}

我在编译过程中遇到错误。我不太确定是什么原因造成的。有任何想法吗?

4

8 回答 8

23

一个 Java 文件可以包含多个类,但只有一个类可以是公共的。一旦public从类中删除关键字,就可以将它们组合成一个 Java 文件。

于 2012-10-30T04:47:02.150 回答
4

冒着喂勺的风险

请阅读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;
        }
    }
}
于 2012-10-30T04:46:29.280 回答
2

是的,您可以将所有类写在一个 .java 文件中,但您必须只有一个公共类(如果文件名和类名相同)

例子:

甲类 { }

乙类 { }

C类{}

于 2012-10-30T12:56:39.720 回答
1

我假设你是非常初学者!只需将所有这些内容复制粘贴到一个文件中即可TheaterDemo.java。并且不要忘记删除public类声明开头的所有关键字。

于 2012-10-30T04:48:54.107 回答
1

一个 Java 文件最多可以包含一个顶级公共类。该公共顶级类可以包含任意数量的公共嵌套类。

您可以通过以下任何一种方法消除编译器错误:

  • 将其他类移动到它们自己的文件中。例如:FirstClass.java、SecondClass.javaThirdClass.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):

  • 该类型由声明该类型的包的其他编译单元中的代码引用。

  • 该类型被声明为公共的(因此可以从其他包中的代码中访问)。

于 2017-06-28T15:20:50.023 回答
0

只需从所有其他类定义中删除 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
于 2012-10-30T04:45:03.730 回答
0

我看到你已经完成了这种实现。请参考

    private class CalcButtonListener implements ActionListener

在你的TheaterWindow课上。

通过这样做,您正在创建内部类,即CalcButtonListener是类的内部TheaterWindow类。您可以将一些概念扩展到其他类。

于 2012-10-30T04:46:58.760 回答
0

java文件中类文件的个数没有限制。

但是每个源代码文件不能有多个公共类。此外,文件的名称必须与公共类的名称匹配。类似地,声明为 public class Dog { } 的类必须位于名为 Dog.java 的源代码文件中。

没有公共类的文件的名称可能与文件中的任何类都不匹配。

于 2018-12-05T08:20:27.920 回答