通过简单计算器回顾内部类

这篇博客介绍了如何将一个简单的Java计算器程序完全改造为面向对象的设计。首先,计算器类被扩展以包含文本框和按钮实例,并且加载界面的方法被分离出来。接着,监听器类被重构,通过持有计算器对象的引用,可以直接操作计算器的文本框进行计算和清空。最后,通过使用内部类进一步优化了监听器的实现,使得内部类可以直接访问外部类的属性,简化了代码结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当前代码

package Lister;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calc {
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class  Calculator extends Frame{
    public Calculator(){
        TextField textField1=new TextField(10);
        TextField textField2=new TextField(10);
        TextField textField3=new TextField(10);

        //1个按钮
        Button button=new Button("=");
        button.addActionListener(new MyCalculatorLiner(textField1,textField2,textField3));
        //1个标签
        Label label=new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        pack();
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }

}
//监听器类
class MyCalculatorLiner implements ActionListener {
    private TextField textField1,textField2,textField3;
    public MyCalculatorLiner(TextField textField1,TextField textField2,TextField textField3,){
        this.textField1=textField1;
        this.textField2=textField2;
        this.textField3=textField3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取两个需要计算的数
        int n1=Integer.parseInt(textField1.getText());
        int n2=Integer.parseInt(textField2.getText());

        //计算两个数的和
        textField3.setText(""+(n1+n2));

        //清除文本框的文本
        textField1.setText("");
        textField2.setText("");
    }
}

完全改造为面向对象

package Lister;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}
//计算器类
class  Calculator extends Frame{
    TextField textField1,textField2,textField3;
    public void loadFrame(){
        //3个文本框
        textField1=new TextField(10);
        textField2=new TextField(10);
        textField3=new TextField(10);

        //1个按钮
        Button button=new Button("=");
        button.addActionListener(new MyCalculatorLiner(this));
        //1个标签
        Label label=new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        pack();
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

//监听器类
class MyCalculatorLiner implements ActionListener {

    //获取计算器这个对象,在一个类组合另一个类
    Calculator calculator=null;
    public MyCalculatorLiner(Calculator calculator){
        this.calculator=calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int n1=Integer.parseInt(calculator.textField1.getText());
        int n2=Integer.parseInt(calculator.textField2.getText());

        calculator.textField3.setText(""+(n1+n2));

        calculator.textField1.setText("");
        calculator.textField2.setText("");

    }
}

内部类

package Lister;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}
//计算器类
class  Calculator extends Frame{
    TextField textField1,textField2,textField3;
    public void loadFrame(){
        //3个文本框
        textField1=new TextField(10);
        textField2=new TextField(10);
        textField3=new TextField(10);

        //1个按钮
        Button button=new Button("=");
        button.addActionListener(new MyCalculatorLiner());
        //1个标签
        Label label=new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        pack();
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
    //监听器类
    //内部类最大的好处,就是可以畅通无阻的使用外部类的属性和方法!
    private class MyCalculatorLiner implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int n1=Integer.parseInt(textField1.getText());
            int n2=Integer.parseInt(textField2.getText());

            textField3.setText(""+(n1+n2));

            textField1.setText("");
            textField2.setText("");

        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值