LauTN 2013-04-25 12:33 采纳率: 0%
浏览 3867

.Java 中多个 class 的使用问题

背景:这是一个教科书上的计算器代码,因为代码中只有第一个是public class,所以我放到同一个.java文件中。

过程:用sublime将代码抄到电脑里,在Eclipse中打开却发现是 class 调用错误。检查了一边发现代码和书中相同,找不到错误的地方,随后我为各个class建了各自的.java文档,想这次如果有错误就是书中代码错了,但……显示正常,运行ok。

坑爹,我把代码粘贴回原来的.java文档,复制文档+换文件夹,拷贝的文件没有显示错误。但是class的名字是黄色未被调用(这不是废话么?不能调用你还运行个什么)。我再点击修复,得到 private static final long serialVersionUID = 1L;

秉着打破砂锅问到底的精神,所以为什么第一次写上代码时是显示错误,复制粘贴之后又 ok 了呢?
private static final long serialVersionUID = 1L; 这个量的意义何在?

而且:只有 DigitButton 调用是 ok 的,剩下的AddButton、SubtractButton、MultiplyButton、ClearButton…ect 全是提示错误。OperatorButton 类被继承也是不可以的。

/** This program implements a simple four-function calculator */
public class come extends Program{

    /* Initializes the user interface */
    public void init(){
        setLayout(new TableLayout(5,4));
        display = new CalculatorDisplay();
        add(display, "gridwidth=4 height=" + BUTTON_SIZE);
        addButtons();
        addActionListeners();
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source instanceof CalculatorButton){
            ((CalculatorButton) source).action(display);
        }
    }

    private void addButtons(){
        String constraint = "width=" + BUTTON_SIZE + " height=" + BUTTON_SIZE;
        add(new DigitButton(7), constraint);
        add(new DigitButton(8), constraint);
        add(new DigitButton(9), constraint);
        add(new AddButton(), constraint);
        add(new DigitButton(4), constraint);
        add(new DigitButton(5), constraint);
        add(new DigitButton(6), constraint);
        add(new SubtractButton(), constraint);
        add(new DigitButton(1), constraint);
        add(new DigitButton(2), constraint);
        add(new DigitButton(3), constraint);
        add(new MultiplyButton(), constraint);
        add(new ClearButton(), constraint);
        add(new DigitButton(0), constraint);
        add(new EqualsButton(), constraint);
        add(new DivideButton(), constraint);
    }

    private static final int BUTTON_SIZE = 40;
    private CalculatorDisplay display;

}



class CalculatorDisplay extends IntField{

    /* Creates a new calculator display that is not directly editable by the user */
    public CalculatorDisplay(){
        setEditable(false);
        setFont(new Font("SansSerif", Font.PLAIN, 24));
        setValue(0);
        startNewValue = false;
        op = null;
    }

    /* Adds a digit to the display, clearing the old value if startNewValue is set */
    public void addDigit(int digit){
        int value = (startNewValue) ? 0 : getValue();
        setValue(10 * value + digit);
        startNewValue = false;
    }

    /* Sets a new operator, applying the previous one if one exists */
    public void setOperator(OperatorButton button){
        if (op == null){
            memory = getValue();
        } else {
            memory = op.apply(memory, getValue());
            setValue(memory);
        }
        op = button;
        startNewValue = true;
    }

    /* private instance variables */
    private OperatorButton op;       /* The last operator button pressed */
    private int memory;              /* The value to which the operator is applied */
    private boolean startNewValue;   /* Set after an operator to start a new value */
}



/*
   This abstract class is the superclass for every calculator button. Every button must define an action method, which is called whenever the button is cliked. 
 */
abstract class CalculatorButton extends JButton {

    /* Creates a new CalculatorButton with the specified name */
    public CalculatorButton(String name) {
        super(name);
        setFont(new Font("SansSerif", Font.PLAIN, 24));
    }

    /* Called when the button is clicked (every subclass must implement this 
       method) 
    */
    public abstract void action(CalculatorDisplay display);
}

/*
   This class is used for each of the digit buttons. The action consists of 
   adding the digit used as a label on the button, which is returned by getText.
*/
class DigitButton extends CalculatorButton {

    /* Creates a new DigitButton for the digit n */
    public DigitButton(int n) {
        super("" + n);
    }

    /* Adds this digit to the display */
    public void action(CalculatorDisplay display) {
        display.addDigit(Integer.parseInt(getText()));
    }
}

/*
   This abstract class is the superclass of the various operator buttons.
   Each concrete subclass must override the apply method.
*/
abstract class OperatorButton extends CalculatorButton{

    /* Creates a new OperatorButton with the specified name */
    public OperatorButton(String name) {
        super(name);
    }
    /* Informs the display that this operator button has been clicked */
    public void action(CalculatorDisplay display) {
        display.setOperator(this);
    }

    /* Applies this operator (every subclass must implement this method) */
    public abstract int apply(int lhs, int rhs);
}

/*
   The classes AddButton, SubtractButton, MultipButton, and DicideButton
   are the same except for their label and the implementation of apply.   
*/
class AddButton extends OperatorButton {
    public AddButton() { super("+"); }
    public int apply(int lhs, int rhs) { return lhs + rhs; }
}

class SubtractButton extends OperatorButton {
    public SubtractButton() { super("-"); }
    public int apply(int lhs, int rhs) { return lhs - rhs; }
}

class MultiplyButton extends OperatorButton {
    public MultiplyButton() { super("x"); }
    public int apply(int lhs, int rhs) { return lhs * rhs; }
}

class DivideButton extends OperatorButton {
    public DivideButton() { super("/"); }
    public int apply(int lhs, int rhs) { return lhs / rhs; }
}

/*
    The EqualsButton class displays the current value. As it happens, this
    Operation can be implemented simply by setting the operator to null.
*/
class EqualsButton extends CalculatorButton {
    public EqualsButton() {
        super("C");
    }

    public void action(CalculatorDisplay display) {
        display.setOperator(null);
        display.setValue(0);
    }
}


public class ClearButton extends CalculatorButton {
    public ClearButton(){
        super("C");
    }

    public void action(CalculatorDisplay display) {
        display.setOperator(null);
        display.setValue(0);
    }
}
  • 写回答

1条回答 默认 最新

  • csxrzeng 2015-06-05 03:10
    关注

    public class ClearButton extends CalculatorButton
    这里不是还有一个public类

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)