简易计算器的优化
发布日期:2021-05-10 00:19:17 浏览次数:27 分类:精选文章

本文共 4545 字,大约阅读时间需要 15 分钟。

下面是改写后的优化内容:

1. 面向对象编程原则:组合大于继承

在面向对象编程中,组合大于继承是避免代码耦合性过高的重要原则。通过组合可以使代码更灵活,维护性更高。

改动前的面向过程写法

// 简易计算器public class TextCalc {    public static void main(String[] args) {        Calculator calculator = new Calculator();        windowClose1(calculator);    }    // 关闭窗口    private static void windowClose1(Frame frame) {        frame.addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });    }}// 计算器类class Calculator extends Frame {    public Calculator() {        TextField tF1 = new TextField(10);        TextField tF2 = new TextField(10);        TextField tF3 = new TextField(10);        Button button = new Button("=");        button.addActionListener(new MyCalculatorListener(tF1, tF2, tF3));        Label label = new Label("+");        setLayout(new FlowLayout());        add(tF1);        add(label);        add(tF2);        add(button);        add(tF3);        pack();        setVisible(true);    }}// 列表类class MyCalculatorListener implements ActionListener {    private TextField tF1, tF2, tF3;    public MyCalculatorListener(TextField tF1, TextField tF2, TextField tF3) {        this.tF1 = tF1;        this.tF2 = tF2;        this.tF3 = tF3;    }    @Override    public void actionPerformed(ActionEvent e) {        int n1 = Integer.parseInt(tF1.getText());        int n2 = Integer.parseInt(tF2.getText());        tF3.setText("" + (n1 + n2));        tF1.setText("");        tF2.setText("");    }}

完全改造为面向对象写法

// 简易计算器public class TextCalc {    public static void main(String[] args) {        Calculator calculator = new Calculator();        calculator.loadFrame();        windowClose1(calculator);    }    // 关闭窗口    private static void windowClose1(Frame frame) {        frame.addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });    }}// 计算器类class Calculator extends Frame {    // 属性    TextField tF1;    TextField tF2;    TextField tF3;    public void loadFrame() {        tF1 = new TextField(10);        tF2 = new TextField(10);        tF3 = new TextField(10);        Button button = new Button("=");        button.addActionListener(new MyCalculatorListener());        Label label = new Label("+");        setLayout(new FlowLayout());        add(tF1);        add(label);        add(tF2);        add(button);        add(tF3);        pack();        setVisible(true);    }}// 列表类class MyCalculatorListener implements ActionListener {    private Calculator calculator;    public MyCalculatorListener(Calculator calculator) {        this.calculator = calculator;    }    @Override    public void actionPerformed(ActionEvent e) {        int n1 = Integer.parseInt(calculator.tF1.getText());        int n2 = Integer.parseInt(calculator.tF2.getText());        calculator.tF3.setText("" + (n1 + n2));        calculator.tF1.setText("");        calculator.tF2.setText("");    }}

2. 内部类

使用内部类可以更好地包装代码,减少全局变量带来的耦合性问题。

在本次改造中,MyCalculatorListener 类被改造为 Calculator 类的内部类。这种方式使得计算器的逻辑更加集中,代码结构更加清晰。

改造后的代码示例

// 简易计算器public class TextCalc {    public static void main(String[] args) {        Calculator calculator = new Calculator();        calculator.loadFrame();        windowClose1(calculator);    }    // 关闭窗口    private static void windowClose1(Frame frame) {        frame.addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });    }}// 计算器类class Calculator extends Frame {    // 属性    TextField tF1;    TextField tF2;    TextField tF3;    // 初始化界面    public void loadFrame() {        tF1 = new TextField(10);        tF2 = new TextField(10);        tF3 = new TextField(10);        Button button = new Button("=");        button.addActionListener(new MyCalculatorListener());        Label label = new Label("+");        setLayout(new FlowLayout());        add(tF1);        add(label);        add(tF2);        add(button);        add(tF3);        pack();        setVisible(true);    }    // 进行计算的内部类    private class MyCalculatorListener implements ActionListener {        @Override        public void actionPerformed(ActionEvent e) {            int n1 = Integer.parseInt(tF1.getText());            int n2 = Integer.parseInt(tF2.getText());            tF3.setText("" + (n1 + n2));            tF1.setText("");            tF2.setText("");        }    }}

这种改造方式通过组合优于继承的思想,使得代码结构更加灵活,减少依赖关系,从而提升系统的扩展性和可维护性。

上一篇:画笔paint
下一篇:事件监听

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月18日 18时08分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

2025版最新一文彻底搞懂大模型 - Agent(非常详细)零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新关于HW护网行动的一些知识,零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新大模型学习路线,零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新大模型开发流程(非常详细)零基础入门到精通,收藏这一篇就够了 2023-01-25
2025版最新大模型微调方法(非常详细)零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新大语言模型的指令微调,零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新小白学习大模型:什么是大模型?零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新常用黑客工具之【Nmap 教程基础】零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新渗透测试和黑客工具列表,零基础入门到精通,收藏这一篇就够了 2023-01-25
2025版最新网络安全等级保护测评指南,零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新运维怎么转行网络安全?零基础入门到精通,收藏这篇就够了 2023-01-25
2025版最新黑客学习网站(非常详细),零基础入门到精通,看这一篇就够了 2023-01-25
2025版网络工程11个高含金量证书(非常详细)零基础入门到精通,收藏这篇就够了 2023-01-25
2025自学成为黑客必读的5本书籍,带你从小白进阶成大佬 2023-01-25
23张图告诉你组建一个网络需要用到哪些硬件设备?路由器、交换机、防火墙是不是就够了? 2023-01-25
#12 btrfs文件系统 2023-01-25
#3194. 去月球 2023-01-25
$scope angular在controller之外调用 2023-01-25
&和&&的区别 2023-01-25
(AS3)BitmapData.draw比BitmapData.copyPixel能做得更多 2023-01-25