java高级语言 - GUI 02 简单计算器的实现 组合 内部类优化

简易计算器

组合大于继承

class A extends B{
  // inheritance
}
class A{
  public B b;
  // combination
  // refer!
}

code

package com.zepei;

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

public class TestCalculator {
    public static void main(String[] args) {
        new Calculator();
    }
}

// calculator
class Calculator extends Frame{
    public Calculator(){
        // 3 textfields
        TextField num1 = new TextField(10);// number of characters
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);

        // 1 button
        Button button = new Button("=");
        button.addActionListener(new MyCalListener(num1,num2,num3));

        // 1 tag/label
        Label label = new Label("+");

        // layout
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
}

// listener class
class MyCalListener implements ActionListener {
    // get 3 variables
    private TextField num1,num2,num3;
    public MyCalListener(TextField num1,TextField num2,TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // get add number and added number
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        // put the result into third text field
        num3.setText(""+(n1+n2));

        // clear the first two text field
        num1.setText("");
        num2.setText("");
    }
}

code optimization – combination

—> object-oriented

package com.zepei;

// optimize testCalculator using combination

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class TestCalculator01 {
    public static void main(String[] args) {
        new Calculator1().loadFrame();
    }
}

// calculator
class Calculator1 extends Frame{
    // attribute
    TextField num1,num2,num3;
    // method
    public void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalListener1(this));
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

// listener class
class MyCalListener1 implements ActionListener {
    // get calculator object
    Calculator1 calculator = null;
    public MyCalListener1(Calculator1 calculator) {
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // get add number and added number
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());

        // put the result into third text field
        calculator.num3.setText(""+(n1+n2));

        // clear the first two text field
        calculator.num1.setText("");
        calculator.num2.setText("");

    }
}

code optimization2 – inner class

The biggest advantage of inner class is it can get outer attributes and methods with no obstruction.

// optimization using inner class

package com.zepei;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class TestCalculator02 {
    public static void main(String[] args) {
        new Calculator1().loadFrame();
    }
}

// calculator
class Calculator2 extends Frame {
    // attribute
    TextField num1, num2, num3;

    // method
    public void loadFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalListener2());
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
    // listener class
    private class MyCalListener2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // get add number and added number
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());

            // put the result into third text field
            num3.setText(""+(n1+n2));

            // clear the first two text field
            num1.setText("");
            num2.setText("");
        }
    }
}

你可能感兴趣的:(javase)