计算器
OOP原则:组合,大于继承
目前代码
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
//3个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//1个标签
Label label = new Label("+");//加号
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取三个变量
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1、获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2、将这个值加法运算后,放到第三个框
num3.setText(""+(n1+n2));
//3、清除前两个框
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象写法
public class TestCalcPlus {
public static void main(String[] args) {
new Calculator1().loadFrame();
}
}
//计算器类
class Calculator1 extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);
num3 = new TextField(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener1(this));
//1个标签
Label label = new Label("+");//加号
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener1 implements ActionListener{
//获取计算器这个对象,在一个类中组合另一个类
Calculator1 calculator1=null;
public MyCalculatorListener1(Calculator1 calculator1) {
this.calculator1=calculator1;
}
@Override
public void actionPerformed(ActionEvent e) {
//1、获得加数和被加数
int n1 = Integer.parseInt(calculator1.num1.getText());
int n2 = Integer.parseInt(calculator1.num2.getText());
//2、将这个值加法运算后,放到第三个框
calculator1.num3.setText(""+(n1+n2));
//3、清除前两个框
calculator1.num1.setText("");
calculator1.num2.setText("");
}
}
内部类
更好的包装
public class TestCalc2 {
public static void main(String[] args) {
new Calculator2().loadFrame();
}
}
//计算器类
class Calculator2 extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);
num3 = new TextField(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener2());
//1个标签
Label label = new Label("+");//加号
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
//内部类的最大的好处就是可以畅通无阻的访问外部类的属性和方法
private class MyCalculatorListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//1、获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2、将这个值加法运算后,放到第三个框
num3.setText("" + (n1 + n2));
//3、清除前两个框
num1.setText("");
num2.setText("");
}
}
}